Commit 7d91b9a1 authored by Sébastien Toque's avatar Sébastien Toque

avoid inline listener + use ViewHolder pattern

parent 921ab24b
......@@ -12,8 +12,8 @@ import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class BrowserAdapter extends ArrayAdapter<File>
implements Comparator<File> {
......@@ -34,63 +34,68 @@ public class BrowserAdapter extends ArrayAdapter<File>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.browser_item,
parent, false);
}
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.browser_item, parent, false);
holder = new ViewHolder();
holder.check = (CheckBox) view.findViewById(R.id.browser_item_selected);
holder.text = (TextView) view.findViewById(R.id.browser_item_dir);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
final File item = getItem(position);
final DatabaseManager dbManager = DatabaseManager.getInstance();
if (item != null && item.getName() != null) {
TextView dirTextView =
(TextView) view.findViewById(R.id.browser_item_dir);
dirTextView.setText(item.getName());
final CheckBox dirCheckBox =
(CheckBox) view.findViewById(R.id.browser_item_selected);
dirCheckBox.setOnCheckedChangeListener(null);
dirCheckBox.setEnabled(true);
dirCheckBox.setChecked(false);
holder.text.setText(item.getName());
holder.check.setOnCheckedChangeListener(null);
holder.check.setTag(item);
holder.check.setEnabled(true);
holder.check.setChecked(false);
List<File> dirs = dbManager.getMediaDirs();
for (File dir : dirs) {
if (dir.getPath().equals(item.getPath())) {
dirCheckBox.setEnabled(true);
dirCheckBox.setChecked(true);
holder.check.setEnabled(true);
holder.check.setChecked(true);
break;
} else if (dir.getPath().startsWith(item.getPath())) {
Log.i(TAG, item.getPath() + " startWith " + dir.getPath());
dirCheckBox.setEnabled(false);
dirCheckBox.setChecked(true);
holder.check.setEnabled(false);
holder.check.setChecked(true);
break;
}
}
dirCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (dirCheckBox.isEnabled() && isChecked) {
dbManager.addDir(item.getPath());
File tmpFile = item;
while (!tmpFile.getPath().equals("/")) {
tmpFile = tmpFile.getParentFile();
dbManager.removeDir(tmpFile.getPath());
}
} else {
dbManager.removeDir(item.getPath());
}
}
});
holder.check.setOnCheckedChangeListener(onCheckedChangeListener);
}
return view;
}
private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final DatabaseManager dbManager = DatabaseManager.getInstance();
File item = (File) buttonView.getTag();
if (item == null)
return;
if (buttonView.isEnabled() && isChecked) {
dbManager.addDir(item.getPath());
File tmpFile = item;
while (!tmpFile.getPath().equals("/")) {
tmpFile = tmpFile.getParentFile();
dbManager.removeDir(tmpFile.getPath());
}
} else {
dbManager.removeDir(item.getPath());
}
}
};
public void sort() {
super.sort(this);
}
......@@ -100,4 +105,8 @@ public class BrowserAdapter extends ArrayAdapter<File>
file2.getName().toUpperCase());
}
static class ViewHolder {
CheckBox check;
TextView text;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment