Android - 从ListActivity中删除项目onClick?

时间:2014-05-06 04:07:31

标签: android onclick

我有一个带有文字Delete的按钮,并放在它创建的列表项旁边,我希望它能够在单击按钮时删除它们的列表项。但是,在实施时,我发现了一个错误,即引用哪个列表项将被删除,以及列表项不再可供编辑。如何在我的代码中引用在单击时删除哪个项目以及再次访问列表项?感谢所有人和任何回复!

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++) 
    {
        lvItems.getChildAt(i).setBackgroundColor(Color.GREEN);        
    }

    LinearLayout vwParentRow = (LinearLayout)v.getParent();
    Button Delbtn = (Button)vwParentRow.getChildAt(1);

}

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();
}
}

函数MyClickHandler是我在XML中设置按钮onClick的功能

            <Button
            android:id="@+id/Delbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="right"
            android:text="Delete"
            android:onClick="myClickHandler" />

1 个答案:

答案 0 :(得分:0)

那么,您要做的是在列表视图中为列表项添加一个按钮。在这里,我学习了如何做到这一点。 http://androidforbeginners.blogspot.in/2010/03/clicking-buttons-in-listview-row.html

希望它有所帮助。