0

我有一个包含项目的列表视图,当被选中并按下按钮时,应该删除它。不确定是否完全选择了这些项目,因为什么都没有发生。

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,这意味着当用户单击列表视图时它没有被识别,它与人们链接的其他解决方案不同。在发布问题之前,我也尝试了解决方案。

4

2 回答 2

0

您可以尝试通过删除数组中的元素来做同样的事情。

        int removeTarget = playerListView.getSelectedItemPosition();
        playerListView.remove(removeTarget);
        playerListAdapter.notifyDataSetChanged();

希望这可以帮助!

于 2016-01-27T02:59:55.413 回答
0
listView.getItemAtPosition(position)

这将返回List提供给的对象Adapter

Adapterremove()类有一个方法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

playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
            TRACK = position;
            Log.i("DraftBuddy", "Selected: " + position);
        }
    });

这是您可以用来从按钮单击调用的代码

public void removePlayer(View v)
{
        if(playerListView.getSelectedItem() != null)
        {

             playerListAdapter.remove(playerListView.getItemAtPosition(TRACK));
             playerListAdapter.notifyDataSetChanged();
        }
}

更新

如果我认为您想从单击的 Listview 中删除一个项目是正确的。

如果这种情况下您可以像这样获得被点击的项目,

playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
              playerListAdapter.remove(playerListView.getItemAtPosition(position));
              playerListAdapter.notifyDataSetChanged();
        }
    });
于 2016-01-27T09:17:19.527 回答