简体   繁体   English

Android-删除ListActivity中的项目

[英]Android - Delete item within ListActivity

I have a Delete button next to each list item in my ListActivity class, and in my delete button i have android:onClick="myClickHandler" . 我的ListActivity类中的每个列表项旁边都有一个Delete按钮,而在Delete按钮中,我有android:onClick="myClickHandler" Right now, i am working on my code within my myClickHandler function to successfully delete the selected list item. 现在,我正在处理myClickHandler函数中的代码,以成功删除所选列表项。 Could someone please give me advice in how i can reference in my code as to which item is being deleted, as well as delete the item upon being clicked? 有人可以给我一些建议,告诉我如何在我的代码中引用要删除的项目以及在单击该项目时将其删除吗? I've provided my ListActivity code.. Thanks for all and any responses! 我已经提供了ListActivity代码。感谢您的所有回复!

public class LyricList extends ListActivity {

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private LyricsDbAdapter mDbHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lyriclist);
    mDbHelper = new LyricsDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    Cursor lyricsCursor = mDbHelper.fetchAllLyrics();
    startManagingCursor(lyricsCursor);
    String[] from = new String[]{LyricsDbAdapter.KEY_TITLE};
    int[] to = new int[]{R.id.text1};
    SimpleCursorAdapter lyrics = 
        new SimpleCursorAdapter(this, R.layout.lyrics_row, lyricsCursor, from, to);
    setListAdapter(lyrics);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createLyric();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

public void myClickHandler(View v) 
{
    ListView lvItems = getListView();
    for (int i=0; i < lvItems.getChildCount(); i++) 
    {
        //incorrectly implemented...getting errors within this
        LinearLayout vwParentRow = (LinearLayout)v.getParent();
        Button Delbtn = (Button)vwParentRow.getChildAt(1);
        DELETE_ID:
        mDbHelper.deleteLyric(lvItems.getChildAt(i)); 

    }

}

private void createLyric() {
    Intent i = new Intent(this, NextActivity.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, NextActivity.class);
    i.putExtra(LyricsDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}
}

You need to 你需要

  1. Delete it from the DB. 从数据库中删除它。
  2. Call requery() on the Cursor. 在游标上调用requery()。
  3. Call notifyDataSetChanged on the adapter. 在适配器上调用notifyDataSetChanged。

Edit: In order to get the ID you should override the bindView function of the adapter, and attach your OnClickListener in there. 编辑:为了获得ID,您应该覆盖适配器的bindView函数,并在其中附加OnClickListener。

How to remove a selected item from ListView using CursorAdapter 如何使用CursorAdapter从ListView中删除选定的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM