我正在尝试创建一个包含目录名的ListView
。 ListView
包含TextView
以显示文件夹名称和checkbox
。
List已成功创建,但当我向上/向下滚动列表时,我遇到了复选框状态的问题。复选框的状态随机变化。
为了保存复选框的状态,我使用HashMap<String, Boolean>,
字符串继续目录的名称和布尔值 - 复选框状态。
以下是代码(抱歉长度): MainActivity.java
public class MainActivity extends ListActivity implements OnClickListener {
Button done, cancel, selectAll;
ArrayList<String> m_list;
HashMap<String, Boolean> m_checkedMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_checkedMap = new HashMap<String, Boolean>();
done = (Button) findViewById(R.id.bDone);
cancel = (Button) findViewById(R.id.bCancel);
selectAll = (Button) findViewById(R.id.bSelAll);
done.setOnClickListener(this);
cancel.setOnClickListener(this);
new AsyncHandler(this).execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, m_list.get(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bDone:
//Update CheckedMap in DB
Toast.makeText(this, m_checkedMap.values().toString(),
Toast.LENGTH_LONG).show();
break;
case R.id.bCancel:
this.finish();
break;
}
}
public class AsyncHandler extends AsyncTask {
Context context;
public AsyncHandler(Context c) {
context = c;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Object doInBackground(Object... arg0) {
getList();
return null;
}
@Override
protected void onPostExecute(Object result) {
// super.onPostExecute(result);
setListAdapter(new ElementAdapter(context, m_list, m_checkedMap));
}
private void getList() {
m_list = new ArrayList<String>();
File root = new File("/");
File[] files = root.listFiles();
for (File f : files) {
if (f.isDirectory() && !f.isHidden()) {
m_list.add(f.getName());
m_checkedMap.put(f.getName(), false);
}
}
Collections.sort(m_list);
}
}
}
适配器类
public class ElementAdapter extends ArrayAdapter {
LinearLayout rowLayout;
ArrayList<String> items = new ArrayList<String>();
TextView tvElement;
HashMap<String, Boolean> checkedMap;
CheckBox cBox;
Context context;
public ElementAdapter(Context c, ArrayList<String> elements,
HashMap<String, Boolean> m_checkedMap) {
super(c, R.layout.row, elements);
this.items = elements;
this.checkedMap = m_checkedMap;
this.context = c;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.row, null);
}
rowLayout = (LinearLayout) view.findViewById(R.id.row);
tvElement = (TextView) view.findViewById(R.id.tvElement);
tvElement.setText(items.get(position));
cBox = (CheckBox) view.findViewById(R.id.checkBox1);
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View paramView) {
if (cBox.isChecked()) {
//checkedMap.put(tvElement.getText().toString(), false);
checkedMap.put(items.get(position), false);
} else if (!cBox.isChecked()) {
//checkedMap.put(tvElement.getText().toString(), true);
checkedMap.put(items.get(position), true);
}
}
});
cBox.setChecked(checkedMap.get(items.get(position)));
return view;
}
}