我正在尝试创建一个目录列表,其中显示了该文件夹的名称列表。如果用户单击目录名称,并且该目录包含子目录,则将调用列出所有子目录的新活动。这个过程将继续下去。 我已经创建了第一页的列表,但现在因为它是一个连续的循环过程,我应该使用哪种方法。 这个父活动可以调用自身,还是应该将startActivity用于结果或ViewFlipper或其他任何东西。请建议。
编辑[已添加代码:]
MainActivity Class
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;
}
}
答案 0 :(得分:0)
请参阅上面的评论“user370305”。