我需要用listview
填充目录中的文件。我可以将文件放入数组中,但似乎无法将它们放入listview
。这是我的代码;我哪里错了?
private void getDir(final String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(String.valueOf(dirPath));
final File[] files = f.listFiles();
myList = (ListView)findViewById(android.R.id.list);
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
// For testing only ---
System.out.println("Files -- "+files[i]);
System.out.println("Files -- "+file);
// End testing ^^^^^
if (!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if (file.isDirectory()) {
item.add(file.getName() + "/");
}
else {
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list);
setListAdapter(fileList);
}
else {
Toast.makeText(this,"No Files",Toast.LENGTH_LONG).show();
}
}
这是我的xml文件。
<ListView
android:id = "@android:id/list"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_above = "@+id/btnSendSave"
android:clickable = "true"
android:focusable = "true"
android:visibility = "visible" />
我需要让你知道,一切似乎都应该做到,但listview
不会填补。
在代码中,有system.out.println(files[i])
表示文件正在放入数组中。
答案 0 :(得分:1)
您的ArrayAdapter
构造函数有多个问题:
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list);
首先,您没有传递item
,因此适配器将为空。
其次,android.R.id.list
不太可能是您在TextView
中使用的行布局中ListView
的ID。
第三,我怀疑R.layout.activity_sendsave
实际上并不是每个ListView
行所需的布局。
请改为尝试:
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
这会为ListView
使用内置的简单行布局,并提供您的单词列表。
以下是使用此方法的示例活动,来自this sample project:
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.list;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewDemo extends ListActivity {
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
@Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(items[position]);
}
}
答案 1 :(得分:0)
您循环浏览文件,并为每个有效的文件执行以下操作:
item.add(file.getName());
但是,执行此操作后,item
永远不会被使用。根据{{3}}你应该这样做:
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, R.layout.activity_sendsave, item);
这将使用您构建的items
数组。
顺便说一句,虽然您的调试方法确保您将项目放入列表中,但您确实应该在构建完整列表之后使用它(您的{{例如,1}}子句可能有问题)。然后,如果您确定列表是正确的,那么您知道无论消费列表是什么都有问题 - 这就是这里发生的事情。
答案 2 :(得分:0)
以为我会展示工作代码。
public class ListViewDemo extends ListActivity {
private TextView selection;
private String root;
private List<String> item = null;
private List<String> path = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// This is the part I added.
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File dir = new File(sdCard.getPath() + "/hvac/");
if (dir.exists()) {
root = new String(String.valueOf(dir));
}
else {
dir.mkdir();
root = new String(String.valueOf(dir));
}
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(String.valueOf(root));
final File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if (file.isDirectory()) {
item.add(file.getName() + "/");
}
else {
item.add(file.getName());
}
}
}
}
else {
Toast.makeText(this, "No Files", Toast.LENGTH_LONG).show();
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item));
selection = (TextView) findViewById(R.id.selection);
}
@Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(item.get(position));
}
}
它运作得很好。感谢所有指向我的方向。