简体   繁体   中英

Android - Delete item from ListActivity onClick?

I have a button with the text Delete , and is placed next to it's created list item, and I want it to be able to delete their list item upon clicking the button. However upon implementation, i've found an error with referencing which list item will be deleted, as well as list items no longer being accessible to edit. How can I reference in my code as to which item is being deleted upon being clicked as well as make list items accessible again? Thanks for all and any responses!

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

The function MyClickHandler is the function i've set the button's onClick to within the XML

            <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" />

SO, what you want to do is to add a button to your list item in your list view. Here, this is where I learnt how to do it. http://androidforbeginners.blogspot.in/2010/03/clicking-buttons-in-listview-row.html

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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