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.
Hope this helps!