[英]notifyDataSetChanged() doesn't update ListView after the adapter being notified
我正在尝试在android中创建一个文件浏览器,为此我有一个带有listview的布局(下面的代码)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvfileExplorer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fileExplorer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<Button
android:id="@+id/bRoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margins"
android:layout_weight=".33"
android:text="@string/root" />
<Button
android:id="@+id/bOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margins"
android:text="@string/ok"
android:layout_weight=".33"/>
<Button
android:id="@+id/bCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margins"
android:text="@string/cancel"
android:layout_weight=".33" />
</LinearLayout>
<ListView
android:id="@+id/lvFileList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margins"
android:choiceMode="multipleChoice">
</ListView>
</LinearLayout>
在我将所有文件和文件夹添加到列表后,我将一个notifyDataSetChanged()添加到列表适配器。 但列表视图不会更新。 您可以在下面看到活动的代码。
public class FileExplorer extends Activity{
private File currentDirectory = new File("/");
private List<String> directoryEntries = new ArrayList<String>();
private ArrayAdapter<String> lvFileExplorerListAdapter;
private Activity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_explorer);
mActivity = this;
lvFileExplorerListAdapter = new ArrayAdapter<String>(mActivity, android.R.layout.simple_list_item_multiple_choice, directoryEntries);
ListView lvFileExplorerList = (ListView)findViewById(R.id.lvFileList);
lvFileExplorerList.setAdapter(lvFileExplorerListAdapter);
Button bCancel = (Button)findViewById(R.id.bCancel);
bCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Button bRoot = (Button)findViewById(R.id.bRoot);
bRoot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
browseToRoot();
}
});
browseToRoot();
}
private void browseToRoot() {
browseTo(new File("/"));
}
private void browseTo(final File aDirectory){
if (aDirectory.isDirectory()){
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
}
}
private void fill(File[] files) {
directoryEntries.clear();
if(this.currentDirectory.getParent() != null)
directoryEntries.add(getString(R.string.upOneLevel));
int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
for (File file : files){
directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
}
lvFileExplorerListAdapter.notifyDataSetChanged();
}
}
重要的是要说清单具有她应该具有的价值。
有什么建议吗? 谢谢
我想出了我的问题......
在布局上,我有包含按钮的linearlayout。 就像你在上面看到的那样,我有一个* match_parent *可以在列表上方绘制。 我将其更改为wrap_content,一切正常。
谢谢你的帮助!
问题出在这里:
directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
您正在向directoryEntries添加项目,而适配器保留其自己的数据副本。 尝试用以下代码替换该行:
lvFileExplorerListAdapter.add(file.getAbsolutePath().substring(currentPathStringLenght)) ;
每当您需要更改列表项时,您必须直接在适配器上操作。
您是否尝试过notifyDatasetChanged后视图无效?
lvFileExplorerList.invalidate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.