我有一个带有项目的列表视图,当选择这些项目时,按下按钮时,应该删除它们。不确定是否正在选择这些项目,因为没有任何事情发生。
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,这意味着当用户点击列表视图时它没有被识别,它与人们链接的其他解决方案不同。在发布问题之前我也尝试了解决方案。
答案 0 :(得分:0)
您可以尝试通过删除数组中的元素来执行相同的操作。
int removeTarget = playerListView.getSelectedItemPosition();
playerListView.remove(removeTarget);
playerListAdapter.notifyDataSetChanged();
希望这有帮助!
答案 1 :(得分:0)
var ids = list1.Select(a => a.id).Intersect(list2.Select(b => b.id));
这会返回提供给listView.getItemAtPosition(position)
的{{1}}中的对象
List
类有一个方法Adapter
来自Adapter
remove()
{/ 1}}
element
类似这样的事情
list
在adapter.remove("here provide the object which you want to remove")
中,您必须跟踪点击了哪个项目, private int TRACK = 0; // make a variable where you can track which item was clicked in the listview
listview
这是您可以使用按钮点击
来调用的代码TRACK
更新
如果我认为你想从Listview中删除一个被点击的项目。
如果是这种情况你可以得到这样点击的项目,
playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
TRACK = position;
Log.i("DraftBuddy", "Selected: " + position);
}
});