我想要的是获得尽可能多的文本视图(listrows)项目数量。循环的手段。请帮帮我CODERS。我只得到最后一项,所以我"硬编码"它
公共类AllNotes扩展了Activity {
Button button;
TextView textview;
// trial copy code from other from net
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_notes);
String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "" + file.length);
Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();
for (int i=0; i < file.length; i++)
{
// trial copy code from other from net
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> planetList = new ArrayList<String>();
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
listAdapter.add(file[0].getName());
listAdapter.add(file[1].getName());
listAdapter.add(file[2].getName());
listAdapter.add(file[3].getName());
listAdapter.add(file[4].getName());
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
Log.d("Files", "FileName:" + file[i].getName());
Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();
}
/*// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter ); */
}
}
答案 0 :(得分:2)
也许问题出在for循环中,你总是创建新的Listview和arrayadapter
尝试使用以下
String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "" + file.length);
Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();
ListView mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> planetList = new ArrayList<String>();
for (int i=0; i < file.length; i++)
{
planetList.add(file[i].getName());
}
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
Log.d("Files", "FileNames:" + planetList);
Toast.makeText(getApplicationContext(), planetList.toString(),Toast.LENGTH_LONG).show();
答案 1 :(得分:1)
正如Der Golem所说,只需使用file [i]但是你还需要在for循环之前声明listAdapter,否则每次遍历循环时它都会重新赋值!
这是一本可以帮助您的快速指南:
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> planetList = new ArrayList<String>();
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
for (int i=0; i < file.length; i++)
{
listAdapter.add(file[i].getName());
}
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
Log.d("Files", "FileName:" + file[i].getName());
Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();
希望这会有所帮助。
答案 2 :(得分:1)
Button button;
TextView textview;
// trial copy code from other from net
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_notes);
String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
File f = new File(path);
File file[] = f.listFiles();
ArrayList<String> planetList = new ArrayList<String>();
for (int i=0; i < file.length; i++) {
planetList.add(file[i].getName());
}
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
}