gpt4 book ai didi

java - Android:目录和子目录列出连续 Activity 问题

转载 作者:行者123 更新时间:2023-12-01 05:14:38 24 4
gpt4 key购买 nike

我正在尝试创建一个目录列表,其中显示文件夹名称的列表。如果用户单击目录名称并且该目录包含子目录,则将调用一个新 Activity ,列出所有子目录。并且这个过程将会持续下去。我已经创建了第一页的列表,但现在由于它是一个连续循环过程,我应该使用哪种方法。这个父 Activity 可以调用自身吗,或者我应该使用 startActivity 作为结果,或者 ViewFlipper 或其他任何东西。请提出建议。

编辑[添加代码:]

主 Activity 类

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;
}
}

最佳答案

请参阅上面“user370305”的评论。

关于java - Android:目录和子目录列出连续 Activity 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11446821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com