我的Android应用程序中有一个listview,它使用listview本身执行两个函数。
拳头,我可以从列表视图中删除一个项目。
其次,我可以在列表视图中重命名。
任何时候发生任何上述情况,
如果列表视图中的最后一项被删除,如何显示不同的XML布局?
adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
dataList = (ListView) mFrame3.findViewById(R.id.lvFiles); //lvfiles from xml layout
dataList.setAdapter(adapter);
dataList.setEmptyView(noFilesDisplayed); //not working
其次,一旦我在列表视图中重命名任何内容,如何更新列表视图以反映更改?
adapter.notifyDataSetChanged(); //not working
我的ContextMenu中有两个选项,删除和重命名。
如果选择删除,则执行以下代码:
if (menuItemIndex == 0) {
if (folder.exists()) {
//File flEachFile = new File(folder.toString() + "/" + currentFileName + ".tp");
flEachFile = new File(folder.toString() + "/" + txt + ".tp");
flEachFile2 = new File(folder.toString() + "/." + txt + ".tp");
if (flEachFile.exists()) {
flEachFile.delete();
}
if (flEachFile2.exists()) {
flEachFile2.delete();
}
adapter.remove(adapter.getItem(info.position)); //updated the list
//adapter.notifyDataSetChanged();
dataList.invalidate();
dataList.setEmptyView(noFilesDisplayed);//should display the noFilesDisplayed layout but it's not.
//getActivity().getActionBar().setSelectedNavigationItem(1);
}
}
这样可以正常工作,因为我删除列表时列表更新。因此,如果我在视图中有两个列表,并且我删除了一个列表更新以显示一个:
adapter.remove(adapter.getItem(info.position)); //updated the list
但是,如果我删除列表中的最后一项,并且我想在此行中显示另一个xml布局:
dataList.setEmptyView(noFilesDisplayed);//should display the noFilesDisplayed layout but it's not.
它不起作用。
=============================================== =======
如果选择重命名,则执行以下代码:
if (menuItemIndex == 1) {
// custom dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.renamefile);
dialog.setTitle("Rename Existing Trip");
currTrip = (TextView) dialog.findViewById(R.id.tvCurrentFileName);
currTrip.setText(txt);
etRT = (EditText) dialog.findViewById(R.id.etRenameTo);
Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
Button btnApply = (Button) dialog.findViewById(R.id.btnApply);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(folder.exists()) {
flEachFile = new File(folder.toString() + "/" + currTrip.getText().toString() + ".tp");
flEachFile2 = new File(folder.toString() + "/." + currTrip.getText().toString() + ".tp");
if (flEachFile.exists()) {
if (etRT.getText().toString().matches("")) {
rTo = (TextView) dialog.findViewById(R.id.tvRenameTo);
rTo.setTextColor(Color.parseColor("#A60000"));
}
else {
File toFile = new File(folder.toString() + "/" + etRT.getText().toString() + ".tp");
flEachFile.renameTo(toFile);
if (flEachFile2.exists()) {
File toFile2 = new File(folder.toString() + "/." + etRT.getText().toString() + ".tp");
flEachFile2.renameTo(toFile2);
}
//adapter.notifyDataSetChanged();
dataList.invalidate();
dialog.dismiss();
//getActivity().getActionBar().setSelectedNavigationItem(1);
}
}
else {
Toast.makeText(getActivity(), "File does not exist", Toast.LENGTH_SHORT).show();
}
}
}
});
// if cancel is clicked, close the custom dialog
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
如果我切换标签并返回此标签,我只能看到更改。
答案 0 :(得分:1)
最好通过其关联的适配器与ListView
进行互动,在本例中为SetRowCustomAdapter
。要删除项目,您应该adapter.remove(position)
,它将自动处理notifyDataSetChanged()
函数并在内部将其从任何数据结构中删除。至于重命名的内容,这取决于您在幕后建模数据的方式在您的适配器中。你能为你的客户适配器提供更多代码吗?你在内部使用数组吗?
<强>更新强>
关于setEmptyView(noFilesDisplayed)
无法正常工作,noFilesDisplayed
与ListView
所在的布局层次结构是否相同?根据我的经验,其他人也已经验证过,空视图(在本例中为noFilesDisplayed
)必须位于同一布局XML文件中,更具体地说,必须位于同一层次结构中。我个人不知道为什么它必须这样,但似乎这是它的唯一工作方式。这是我的意思的例子......
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<View
android:id="@+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
然后在代码中,您可以将noFilesDisplayed
指向资源ID empty_view
。我不完全确定是否有必要设置gone
的可见性,尝试使用它。我希望能回答你关于空视图的问题。
答案 1 :(得分:1)
adpater.notifyDataSetChanged();
,如果notifyDataChanged()
仍然不起作用,则可以设置适配器并将新更新的数据传递给onResume
中的适配器构造函数方法。因此,无论何时删除/重命名listView中的数据,它都会自动更新。 (仅当您开始删除或重命名listView项目的新活动时,此第二种方法才有效。)
@Override
protected void onResume() {
super.onResume();
SetRowsCustomAdapter adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
dataList.setAdapter(adapter);
}