我正在尝试创建一个目录列表,其中显示了文件夹名称的列表。如果用户单击目录名称并且该目录包含子目录,则将调用一个新活动来列出所有子目录。而这个过程将继续下去。我已经创建了第一页的列表,但现在因为它是一个连续的循环过程,我应该使用哪种方法。这个父活动可以自己调用吗,或者我应该使用 startActivity 作为结果还是 ViewFlipper 或其他任何东西。请建议。
编辑[代码添加:]
MainActivity 类
public class MainActivity extends ListActivity implements OnClickListener {
Button done, cancel, selectAll;
ListView listView;
ArrayAdapter<Directory> adapter;
List<Directory> list;
HashMap<String, Boolean> checkedMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
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();
if (list != null) {
checkedMap = new HashMap<String, Boolean>();
Iterator<Directory> i = list.iterator();
while (i.hasNext()) {
Directory d = (Directory) i.next();
checkedMap.put(d.getName(), d.isSelected());
}
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// super.onListItemClick(l, v, position, id);
Directory d = list.get(position);
File[] subDir = d.getFile().listFiles();
// TODO: UPDATE ACTIVITY
}
@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, list));
}
private void getList() {
list = new ArrayList<Directory>();
File root = new File("/");
File[] files = root.listFiles();
for (File f : files) {
if (f.isDirectory() && !f.isHidden()) {
list.add(new Directory(f, f.getName()));
}
}
}
}
}
自定义适配器
public class ElementAdapter extends ArrayAdapter {
private Context context;
private List<Directory> list;
public ElementAdapter(Context c, List<Directory> list) {
super(c, R.layout.row, list);
this.context = c;
this.list = list;
}
static class ViewHolder {
protected TextView textView;
protected CheckBox checkBox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View view = convertView;
if (view == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.tvElement);
viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
viewHolder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(
buttonView.isChecked());
}
});
view.setTag(viewHolder);
view.setTag(R.id.tvElement, viewHolder.textView);
view.setTag(R.id.checkBox1, viewHolder.checkBox);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.checkBox.setTag(position);
viewHolder.textView.setText(list.get(position).getName());
viewHolder.checkBox.setChecked(list.get(position).isSelected());
return view;
}
}