1

I have a single screen with a bank of buttons below a ListView. Entries on the ListView light up in orange when I scroll so I assume that are selected. When I then press the "Delete" button I want the onClickListener to remove the currently selected entry. But getSelectedItemPosition() always gives me -1. If I can't hope to use the GUI controls in this way, please give me another way of getting the same result.

I have even tried setting the onClickListener of the List View to store the index before the button is pressed (in case pressing the button unselects the entry) but even that is always -1 it seems.

Here's the code (without the modification which didn't work)

package com.bayley;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;

/**
 *
 * @author p0074564
 */
public class September extends Activity {

 /** Called when the activity is first created. */


 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  final ListView myListView = (ListView) findViewById(R.id.myListView);
  Button addButton = (Button) findViewById(R.id.AddButton);
  Button deleteButton = (Button) findViewById(R.id.DeleteButton);
  final EditText editText = (EditText) findViewById(R.id.myEditText);

  final ArrayList<String> todoItems = new ArrayList<String>();
  todoItems.add("Monday");
  todoItems.add("Tuesday");
  todoItems.add("Wednesday");

  final ArrayAdapter<String> aa =
   new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, todoItems);
  myListView.setAdapter(aa);

  addButton.setOnClickListener(new 
    Button.OnClickListener() {
   public void onClick(View v) {
    todoItems.add(editText.getText().toString());
    aa.notifyDataSetChanged();
   }
  });


  deleteButton.setOnClickListener(new 
    Button.OnClickListener() {
   public void onClick(View v) {
    // always returns -1 unfortunately ie nothing is ever selected
    int index = myListView.getSelectedItemPosition();
    if (index >= 0) {
     todoItems.remove(index);
    }
    aa.notifyDataSetChanged();
   }
  });



 }
}
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
Ian Bayley
  • 11
  • 2
  • Are you sure that the item is really selected. I think it might only be focused. – Flo Sep 09 '10 at 08:40
  • OK, you might be right. It is highlighted in orange. I thought that meant it was selected. Perhaps I am mistaken in my vocabulary. Come to think of it, maybe it needs to be double clicked to be selected, but then a button click would unselect it again. Is there a way to find out which item is focused? Or if clicking the delete button loses the focus, perhaps I could override an onFocus event to save the focussed index? – Ian Bayley Sep 09 '10 at 10:38
  • There is an OnFocusChangedListener interface, but I don't know if you can attach this listener to the list items, but I think it should be possible some how. But it might not help you because the focus will be lost when the button is clicked. For two other ways to handle you problem look at my answer below. – Flo Sep 09 '10 at 11:20

2 Answers2

0

As I already mentioned in my comment I don't know if you can attach a OnFocusChangedListener to the items of a list, but I pretty sure that is possible some how, although it won't really help you.

But perhaps the both option below will might be interesting for you.

  1. Implement a item context menu which appears when you long click an item. In this context menu you can provide a delete action. You will see this behavior on many different Android apps which handle some sort of lists. Have a look at this blog post.

  2. Make you list to be capable of selecting multiple items. See this question for more infos. By this way you can delete multiple items at once.

Community
  • 1
  • 1
Flo
  • 27,355
  • 15
  • 87
  • 125
0

i some what modified ur code.. its working

public class Selectlistitem extends Activity {
    int pos;

 /** Called when the activity is first created. */


 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  final ListView myListView = (ListView) findViewById(R.id.widget34);
  Button addButton = (Button) findViewById(R.id.btnadd);
  Button deleteButton = (Button) findViewById(R.id.btnremove);
  final EditText editText = (EditText) findViewById(R.id.txt1);

  final ArrayList<String> todoItems = new ArrayList<String>();
  todoItems.add("Monday");
  todoItems.add("Tuesday");
  todoItems.add("Wednesday");

  final ArrayAdapter<String> aa =
   new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, todoItems);
  myListView.setAdapter(aa);

  addButton.setOnClickListener(new
    Button.OnClickListener() {
   public void onClick(View v) {
    todoItems.add(editText.getText().toString());
    aa.notifyDataSetChanged();
   }
  });

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
        aa.getItem(pos);
        editText.setText(""+pos);
    }
});
  deleteButton.setOnClickListener(new
    Button.OnClickListener() {
   public void onClick(View v) {
    // always returns -1 unfortunately ie nothing is ever selected
//    int index = myListView.getCheckedItemPosition();
      int index=pos;
    if (index >= 0) {
     todoItems.remove(index);
    }
    editText.setText(""+index);
    aa.notifyDataSetChanged();
   }
  });



 }
}
Bert F
  • 85,407
  • 12
  • 106
  • 123
jerith
  • 141
  • 1
  • 7
  • 18
  • Thanks a lot. I think I was using OnClickListener instead of OnItemClickListener. I was very surprised that pressing the arrow key causes an "item click" event in the item into which it is moved. The only remaining slight problem is that I would like an item to stay orange when I click on it so that I know which item would be deleted if I were to press delete. I thought that maybe myListView.setItemChecked(pos, true); (in onItemClick) would give me this but it doesn't. Any ideas, please? – Ian Bayley Jan 13 '11 at 18:41