[Android] Convert some for loops to foreach loops in FileBrowser.java.

Also make an anonymous class method parameter more readable. Anything is better than arg[x].
This commit is contained in:
Lioncash 2014-03-06 03:00:31 -05:00
parent 6e65e8d565
commit 19b23a411f
1 changed files with 20 additions and 25 deletions

View File

@ -8,6 +8,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -196,11 +197,8 @@ public class FileBrowser extends Fragment {
} }
FileUtils fileUtils = new FileUtils(); FileUtils fileUtils = new FileUtils();
File[] allMatchingFiles = fileUtils.listFilesAsArray(storage, File[] allMatchingFiles = fileUtils.listFilesAsArray(storage, filter, 1);
filter, 1); Collections.addAll(tFileList, allMatchingFiles);
for (File mediaFile : allMatchingFiles) {
tFileList.add(mediaFile);
}
return tFileList; return tFileList;
} }
@ -214,8 +212,8 @@ public class FileBrowser extends Fragment {
.getString(R.string.games_listing); .getString(R.string.games_listing);
createListHeader(heading, list, true); createListHeader(heading, list, true);
if (games != null && !games.isEmpty()) { if (games != null && !games.isEmpty()) {
for (int i = 0; i < games.size(); i++) { for (final File game : games) {
createListItem(list, games.get(i)); createListItem(list, game);
} }
} else { } else {
Toast.makeText(parentActivity, R.string.config_game, Toast.makeText(parentActivity, R.string.config_game,
@ -351,7 +349,6 @@ public class FileBrowser extends Fragment {
createListHeader(heading, v, false); createListHeader(heading, v, false);
File flist[] = root_sd.listFiles(); File flist[] = root_sd.listFiles();
File parent = root_sd.getParentFile(); File parent = root_sd.getParentFile();
list.add(null); list.add(null);
@ -361,32 +358,30 @@ public class FileBrowser extends Fragment {
Arrays.sort(flist, new DirSort()); Arrays.sort(flist, new DirSort());
for (int i = 0; i < flist.length; i++) Collections.addAll(list, flist);
list.add(flist[i]);
for (int i = 0; i < list.size(); i++) { for (final File file : list) {
if (list.get(i) != null && !list.get(i).isDirectory()) if (file != null && !file.isDirectory())
continue; continue;
final View childview = parentActivity.getLayoutInflater().inflate( final View childview = parentActivity.getLayoutInflater().inflate(
R.layout.app_list_item, null, false); R.layout.app_list_item, null, false);
if (list.get(i) == null) { if (file == null) {
((TextView) childview.findViewById(R.id.item_name)) ((TextView) childview.findViewById(R.id.item_name))
.setText(getString(R.string.folder_select)); .setText(getString(R.string.folder_select));
} else if (list.get(i) == parent) } else if (file == parent)
((TextView) childview.findViewById(R.id.item_name)) ((TextView) childview.findViewById(R.id.item_name))
.setText(".."); .setText("..");
else else
((TextView) childview.findViewById(R.id.item_name)) ((TextView) childview.findViewById(R.id.item_name))
.setText(list.get(i).getName()); .setText(file.getName());
((ImageView) childview.findViewById(R.id.item_icon)) ((ImageView) childview.findViewById(R.id.item_icon))
.setImageResource(list.get(i) == null ? R.drawable.config .setImageResource(file == null ? R.drawable.config
: list.get(i).isDirectory() ? R.drawable.open_folder : file.isDirectory() ? R.drawable.open_folder
: R.drawable.disk_unknown); : R.drawable.disk_unknown);
childview.setTag(list.get(i)); childview.setTag(file);
final File item = list.get(i);
orig_bg = childview.getBackground(); orig_bg = childview.getBackground();
@ -395,8 +390,8 @@ public class FileBrowser extends Fragment {
childview.findViewById(R.id.childview).setOnClickListener( childview.findViewById(R.id.childview).setOnClickListener(
new OnClickListener() { new OnClickListener() {
public void onClick(View view) { public void onClick(View view) {
if (item != null && item.isDirectory()) { if (file != null && file.isDirectory()) {
navigate(item); navigate(file);
ScrollView sv = (ScrollView) parentActivity ScrollView sv = (ScrollView) parentActivity
.findViewById(R.id.game_scroller); .findViewById(R.id.game_scroller);
sv.scrollTo(0, 0); sv.scrollTo(0, 0);
@ -433,11 +428,11 @@ public class FileBrowser extends Fragment {
childview.findViewById(R.id.childview).setOnTouchListener( childview.findViewById(R.id.childview).setOnTouchListener(
new OnTouchListener() { new OnTouchListener() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean onTouch(View view, MotionEvent arg1) { public boolean onTouch(View view, MotionEvent event) {
if (arg1.getActionMasked() == MotionEvent.ACTION_DOWN) { if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(0xFF4F3FFF); view.setBackgroundColor(0xFF4F3FFF);
} else if (arg1.getActionMasked() == MotionEvent.ACTION_CANCEL } else if (event.getActionMasked() == MotionEvent.ACTION_CANCEL
|| arg1.getActionMasked() == MotionEvent.ACTION_UP) { || event.getActionMasked() == MotionEvent.ACTION_UP) {
view.setBackgroundDrawable(orig_bg); view.setBackgroundDrawable(orig_bg);
} }