我正在创建一个显示目录名称的 multiple_choice 列表视图。由于它是多选选择,每个项目都包含一个文本视图和一个复选框。默认情况下,当我单击任何项目时,复选框会被选中/取消选中。这不是我想要的功能。因为,我正在显示目录的名称,所以我希望当用户单击该项目时,而不是选中复选框,应该显示一个新列表,其中包含该目录的子文件夹的名称。
In simple terms, when the textview of the item is selected a new list should get displayed & when the the check box is selected then only the check box should be checked/unchecked.
下面是我到目前为止的代码:
public class DirectoryListing extends Activity implements OnItemClickListener {
ListView dirlv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directorylisting);
try {
ArrayList<String> dirList = new ArrayList<String>();
dirList = getAllDirList();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, dirList);
dirlv = (ListView) findViewById(R.id.lvDirList);
dirlv.setAdapter(adapter);
dirlv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dirlv.setOnItemClickListener(this);
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
private ArrayList<String> getAllDirList() {
ArrayList<String> result = new ArrayList<String>();
File home = new File("/");
File[] files = home.listFiles();
for (File file : files) {
if (file.isDirectory()) {
result.add(file.getName());
}
}
return result;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try {
TextView tv = (TextView) view;
//TODO
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}