简体   繁体   中英

Removing Item from list view

I have a list view with items, that when selected and when a button is pressed, should be deleted. Not sure if the items are being selected at all because nothing is happening.

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

The Code for the button

public void removePlayer(View v)
{
        if(playerListView.getSelectedItem() != null)
        {
            int removeTarget = playerListView.getSelectedItemPosition();
            playerListAdapter.remove(playerListAdapter.getItem(removeTarget));
            playerListAdapter.notifyDataSetChanged();
        }
}

The code for the view:

<?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>

the log statement is not being printed out to logcat which means it is not being recognized when the user clicks on the list view it is different from the other solutions people have linked. I also tried the solutions before posting the problem.

You can try doing the same thing by instead removing the element in the array.

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

Hope this helps!

listView.getItemAtPosition(position)

This return the Object in the List which is provided to the Adapter ,

Adapter class has a method to remove() a element from the provided list

adapter.remove("here provide the object which you want to remove")

something like this

 private int TRACK = 0; // make a variable where you can track which item was clicked in the listview 

in your listview you have to keep track of which item was clicked, 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);
        }
    });

this is code which you can use to call from the button click

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

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

Update

if I believe right that you want to remove the an item from the Listview which one is clicked.

if this case you can get the item which is been clicked like this,

playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
              playerListAdapter.remove(playerListView.getItemAtPosition(position));
              playerListAdapter.notifyDataSetChanged();
        }
    });

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