从列表中删除已删除的项目

时间:2014-10-24 08:52:59

标签: android

我有一个字符串列表视图。

我想删除已删除的项目。

在下面的代码中,该项目在数据库中被删除(关闭并重新开启确认这一点)。

这里我想删除已删除的项目

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
     int menuItemIndex = item.getItemId();
     final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); 
     final int pos = menuInfo.position;
      String menuItemName =menuItems[menuItemIndex];
     if(menuItemName=="Delete")
     {
         try{
         Locations l= (Locations)locations.get(pos);
         int id= l.get_id();
        if( db.deleteLocation(id))
        {
         locations.remove(pos);
         arrayAdapter.notifyDataSetChanged();
        }else
        {
            Toast.makeText(this,"Can not delet a location  with apartment blocks",Toast.LENGTH_LONG).show();
        }

         }catch(Exception e)
         {
             e.printStackTrace();
         }
     }
    return super.onContextItemSelected(item);
}

这是整个活动的代码。

注意该项目在数据库中被删除但仍然显示,直到我关闭并重新打开列表。

package com.example.metermanager;

import java.util.ArrayList;
import java.util.List;

import meter.manager.helper.DatabaseHelperClass;
import meters.model.Locations;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ApartmentLocations extends Activity {
    private DatabaseHelperClass db; 
    private List<Locations> locations= new ArrayList<Locations>();
     String[] menuItems = {"Delete"};
     ArrayAdapter<String> arrayAdapter ;
    ListView list;
@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.appartment_locations);
    db= DatabaseHelperClass.getInstance(this);
    list= (ListView) findViewById(R.id.apartments_locations);   
    locations=db.GetLocations();
    List <String> s = new ArrayList<String>();

    for (Locations l:locations){
        s.add(l.toString());
    }
     arrayAdapter = new ArrayAdapter<String>(
            this, 
            android.R.layout.simple_list_item_1,
            s );

    list.setAdapter(arrayAdapter); 
    registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.apartments_locations) {       
        menu.setHeaderTitle("Choose a menu item");             
        for (int i = 0; i<menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
     int menuItemIndex = item.getItemId();
     final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); 
     final int pos = menuInfo.position;
      String menuItemName =menuItems[menuItemIndex];
     if(menuItemName=="Delete")
     {
         try{
         Locations l= (Locations)locations.get(pos);
         int id= l.get_id();
        if( db.deleteLocation(id))
        {
         locations.remove(pos);
         arrayAdapter.notifyDataSetChanged();
        }else
        {
            Toast.makeText(this,"Can not delet a location  with apartment blocks",Toast.LENGTH_LONG).show();
        }

         }catch(Exception e)
         {
             e.printStackTrace();
         }
     }
    return super.onContextItemSelected(item);
}
}

任何线索?

罗纳德

3 个答案:

答案 0 :(得分:1)

您可以使用两件事来实现:

  1. 当您使用arrayAdapter.notifyDataSetChanged();时,它仅在适配器中传递的数据(列表)被打开时才有效。所以在使用之前你应该在你的情况下从传递的arraylist中删除数据s(list)。
  2. 所以你应该全局声明列表变量s,然后在需要时删除项目,如:

         locations.remove(pos);
         s.remove(pos);
         arrayAdapter.notifyDataSetChanged();
    

    OR 2.您可以使用将在您的上下文中设置的SimpleCursorAdapter http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

    而且,我又发现了一个错误:
    if(menuItemName=="Delete") 我们不能像这样比较java中的字符串所以请将其改为如下:
        if(menuItemName.equals("Delete"))

答案 1 :(得分:0)

使用CursorAdapter而不是ArrayAdapter。 更多信息:http://developer.android.com/reference/android/widget/CursorAdapter.html

答案 2 :(得分:0)

只需在执行删除命令的ur活动中的notifydatasetchanged()之后添加locations.clear()

相关问题