Android: how to remove row from ListView with delete button in row

I know that there are many threads with more or less than one topic, but none of them covers my situation:

I populate my ListView with a special CursorAdapter using my own rows. I have two states for a ListView: the first state consists only of texts in rows; the second state has a delete button in each row. Switching between two states is the button at the bottom of the view.

I am trying to achieve: in the first state, when I click on a line, the screen should switch to another view. In the second state, when I click the delete button, the selected row must be deleted from the database, the ListView must be populated, and nothing should happen if I click any other part of the row except the delete button.

I know how to delete an item and refill the ListView, and I know how to switch to another view when I click on a row in the first state; but I could not combine them. I use the following method to get id and line position:

protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //Code goes in here either for deleting or switching into another view } 

I tried using some if statements to get a condition for clicking the delete button or for clicking on a line, but that didn't work.

The first question is obvious: how can I make it work?

The second question: in the second state, when any part of the line is pressed, except for the button of the "Delete" button, it switches to another view. How can I prevent this? I tried to set the .setClickable, .setPressed, .set Selected string attributes to false, but that didn't help.

Thanks for your answers in advance.

+7
source share
2 answers

In the second state, when any part of the line is pressed, except for the button of the delete button, it switches to another view. How can I prevent this?

You just have a listener for deleteButton

  @Override public View getView(int position, View convertView, ViewGroup parent) { View row = null; LayoutInflater inflater = getLayoutInflater(); row = inflater.inflate(R.layout.one_result_details_row, parent, false); // inflate other items here : Button deleteButton = (Button) row.findViewById(R.id.Details_Button01); deleteButton.setTag(position); deleteButton.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { Integer index = (Integer) view.getTag(); items.remove(index.intValue()); notifyDataSetChanged(); } } ); 

In getView () method you put ListItem in postion

 deleteButton.setTag(position); 

Convert getTag() to an integer

In OnClickListener() you will remove the item

  items.remove(index.intValue()); 
+25
source

I think the code below is better for the second question:

 Button ic_delete = (Button) row.findViewById(R.id.ib_delete); ic_delete.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // delete row from database notifyDataSetChanged(); } }); 
0
source

All Articles