1

我在列表视图上创建了数据库和 Y 显示记录,如果我长按列表视图记录,则上下文菜单打开。在上下文菜单中,有两个操作,一是显示操作,二是删除操作。如果我选择显示,则在下一个活动中显示完整记录。

ListActivity.java

public class ListActivity extends Activity {
    ListView lv;
    ArrayList<String> items = new ArrayList<String>();
    ArrayList<String> items_id = new ArrayList<String>();
    dbhelper dh;
    SQLiteDatabase db;
    int index_id;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Button btn;
        btn = (Button)findViewById(R.id.newProjectlist);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub


                    Intent intent= new Intent(ListActivity.this,NewProject.class);
                startActivity(intent);
            }
        });  
        lv = (ListView)findViewById(R.id.projectListView);
        setUpList();
        dh = new dbhelper(this);
        Cursor c = getAllData();
        showAllData(c);  
        registerForContextMenu(lv);      
    }

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

        if(v.getId() == R.id.projectList){
            menu.setHeaderIcon(R.drawable.ic_launcher);
            menu.setHeaderTitle("Record List");
            menu.add(0,1,menu.NONE,"Delete Record");
            menu.add(0,2,menu.NONE,"Show Record");
        }
    }

    @Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
        int itemId = item.getItemId();
        if(itemId == 1)
        {
            AdapterView.AdapterContextMenuInfo menuinfo;
            menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
            index_id = menuinfo.position;
            deleteUserData(index_id);
        }
        if(itemId == 2){
            AdapterView.AdapterContextMenuInfo menuinfo;
            menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
            index_id = menuinfo.position;
            editUserData(index_id);
        }
    return super.onContextItemSelected(item);
}

    private void editUserData(int indexid) {
        // TODO Auto-generated method stub
        String user_id = items_id.get(indexid);
        Intent intEdit = new Intent(ListActivity.this, MainActivity.class);
        intEdit.putExtra("EditID", user_id);
        startActivityForResult(intEdit, 1);
    }

    private void deleteUserData(int indexid) {
        // TODO Auto-generated method stub
        String user_id = items_id.get(indexid);
        //Toast.makeText(getApplicationContext(), "id " + user_id, 1).show();
        db = dh.getWritableDatabase();
        db.delete("timer","_ID" + "=" + user_id, null);
        finish();
//      Toast.makeText(getApplicationContext(), "Record deleted successfully", 1).show();
    }

    private void showAllData(Cursor c) {
        // TODO Auto-generated method stub
        while(c.moveToNext())
        {
            int id = c.getInt(0);
            String name = c.getString(1);
            String timerpoint1 = c.getString(2);
            String timerpoint2 = c.getString(3);
            String timerpoint3 = c.getString(4);
            String servicetime1 = c.getString(5);
            String servicetime2 = c.getString(6);
            items.add("" + name);
            items_id.add(""+id);
        }
        setUpList();
    }

    private Cursor getAllData() {
        // TODO Auto-generated method stub
        db = dh.getReadableDatabase();
        //String sortorder = dh.Name + "DESC";
    //  String query ="SELECT DISTINCT name FROM timer";
    //  Cursor c = db.rawQuery(query, null);
    //   if (c != null) {
    //            c.moveToFirst();
    //        }
        String[] cols = new String[] {dh.Name, "name"};
        Cursor c = db.query(true, "timer",null,null,null,null,"ASC",null, null);   
        //Cursor cur = db.query(Distinct,"timer", null, null, null, null, null, null, null);
        startManagingCursor(c);
        return c;   
        }
    private void setUpList() {
        // TODO Auto-generated method stub
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
    }   

    }

如果我选择删除然后删除记录。使用上下文菜单我们可以实现这个目标。我有该应用程序的一些代码,但代码无法正常工作。所以,请为此提供帮助。

4

2 回答 2

0

请提供有关您所看到内容的更多详细信息,但是查看您的代码我猜测列表视图可能保持相同状态,并且在您删除后不刷新。如果是这种情况,您可能需要重新查询光标。此方法已弃用,但是您应该能够使用它来验证是否是问题所在,或者只需获取光标的新实例并按照文档建议加载它。

于 2013-01-02T03:34:59.507 回答
0

有几件事:首先你的删除方法是关闭的。您获得了 id 字段,然后找到了要删除的相应字符串,但您仍然希望_id使用此字符串在字段中删除。这种方式不会删除任何内容,因为这些字符串不会在该列中。更好的是简单地删除原来的long id

private void deleteUserData(long indexid) {
    db.delete("timer","_ID" + "=" + indexid, null);
    finish();
}

就是这样。现在另一个问题实际上是检索要删除的 id。你正在使用

index_id = menuinfo.position;

但这不会给您数据库中的任何 ID。这只是给你ListView适配器上的位置。你需要使用这个位置,来得到这个_id。由于您使用的是,ArrayAdapter这意味着_id从用于填充此列表视图的相应集合中获取。

所以根据你的人口方法。

private void showAllData(Cursor c) {
    while(c.moveToNext())
    {
        int id = c.getInt(0);
        String name = c.getString(1);
        .....
        items.add("" + name);
        items_id.add(""+id);
    }
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}

很明显,您现在需要做什么才能_id根据现在的位置得到这个。

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if(itemId == 1)
        {
            AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
            int position = menuinfo.position;
            String id_string = items_id.get(position);
            long id = Long.valueOf(id_string);
            deleteUserData(id);
//          then you probably want it off the corresponding collection too
            items_id.remove(position);
        }
        .....
于 2013-01-02T03:48:55.327 回答