Android-是否从ListActivity onClick中删除项目?
我有一个按钮,文本为“删除”,它位于它创建的列表项旁边,我希望它能够在单击该按钮后删除它们的列表项。然而,在实现时,我发现了一个错误:引用将被删除的列表项,以及无法再编辑的列表项。我如何在代码中引用单击时删除的项目,以及如何再次访问列表项目?谢谢你的回复Android-是否从ListActivity onClick中删除项目?,android,onclick,Android,Onclick,我有一个按钮,文本为“删除”,它位于它创建的列表项旁边,我希望它能够在单击该按钮后删除它们的列表项。然而,在实现时,我发现了一个错误:引用将被删除的列表项,以及无法再编辑的列表项。我如何在代码中引用单击时删除的项目,以及如何再次访问列表项目?谢谢你的回复 public class LyricList extends ListActivity { private static final int ACTIVITY_CREATE=0; private static final int ACTIVI
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();
}
}
因此,您要做的是在列表视图的列表项中添加一个按钮。在这里,我学会了如何做。
希望有帮助。在onListItemClick方法中,Long id参数是被单击项的RowId。只要把它送到你想要的地方就行了。谢谢你的回复!但我认为这与使用按钮是分开的。你对如何在onClick中使用长id作为按钮有什么建议吗?每个项目都有delete按钮,或者在listview下面/上面只有一个delete按钮?每个项目都有delete按钮Tanks,我正在从该资源实现代码。。。它现在正在崩溃,但如果我让它工作,它会让你知道:)如果它真的工作,请标记这是一个答案。更新你的问题,如果你不能让它工作,我们来看看,看看是怎么回事。
<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" />