我有一个包含项目的列表视图,当被选中并按下按钮时,应该删除它。不确定是否完全选择了这些项目,因为什么都没有发生。
currentDraft = new Draft();
inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
playerListView = (ListView)this.findViewById(android.R.id.content).getRootView().findViewById(R.id.player_list);
playerListAdapter = new ArrayAdapter(this,R.layout.player_item,R.id.player_name_txt, new ArrayList<String>(){});
playerListView.setAdapter(playerListAdapter);
playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
playerListView.setSelection(position);
Log.i("DraftBuddy", "Selected: " + position);
}
});
按钮的代码
public void removePlayer(View v)
{
if(playerListView.getSelectedItem() != null)
{
int removeTarget = playerListView.getSelectedItemPosition();
playerListAdapter.remove(playerListAdapter.getItem(removeTarget));
playerListAdapter.notifyDataSetChanged();
}
}
视图的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mtgdraftbuddy.bryantyoung.draftbuddy2.DraftStart"
android:clickable="true"
android:contextClickable="true"
android:background="#0a0909">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/player_list"
tools:listitem="@android:layout/simple_list_item_1"
android:layout_alignParentTop="true"
android:layout_above="@+id/add_player_btn"
android:layout_alignRight="@+id/Startbtn"
android:layout_alignEnd="@+id/Startbtn"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player"
android:id="@+id/add_player_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:onClick="addPlayer" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Draft"
android:id="@+id/Startbtn"
android:layout_alignTop="@+id/add_player_btn"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/removeBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="removePlayer" />
</RelativeLayout>
日志语句没有被打印到 logcat,这意味着当用户单击列表视图时它没有被识别,它与人们链接的其他解决方案不同。在发布问题之前,我也尝试了解决方案。