嗨我工作的是一個簡單的文件瀏覽器,是我的應用程序的一部分,我已經添加了一些代碼來刪除文件,但我需要的文件列表後刷新一個項目刪除。這是我到目前爲止的代碼,並且在我撥打電話fileList.notifyDataSetChanged();
的地方添加了一條評論,但它對我無效,所以基本上我在這裏做錯了什麼?感謝您的幫助幫助刷新清單與notifyDataSetChanged()
private void getDir(String dirPath)
{
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
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.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
final File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.boot)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
else
{
final CharSequence[] items = {"Info", "Rename", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options for " + file.getName());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
builder.setIcon(R.drawable.info)
.setTitle(file.getName() + " info")
.setMessage("Path: " + file.getPath() + "\n\nSize: " + (double)file.length()/1024 + " mbs" + "\n\nModified: " + sdf.format(file.lastModified()))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
else if (item == 1)
{
//TODO Add Rename code here
Toast.makeText(Installed.this, "Rename", Toast.LENGTH_SHORT).show();
}
else if (item == 2)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this);
builder.setMessage("Are you sure you want to delete " + file.getName() +"?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
File f1 = new File(file.getPath());
boolean success = f1.delete();
if (!success){
Toast.makeText(Installed.this, "Could not delete " + file.getName(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(Installed.this, file.getName() + " deleted!", Toast.LENGTH_SHORT).show();
}
//This is where I try to refresh the list
fileList.notifyDataSetChanged();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
}
}).show();
}
}
檢查了這一點: http://stackoverflow.com/questions/4088862/android-list-view-refresh/4088889#4088889閱讀註釋 – blindstuff 2011-03-16 23:43:36
任何一個可以幫助HTTP://計算器。 com/questions/28148618/listview-not-refreshing-after-click-on-button – 2015-01-28 04:34:21