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();
}
});
dele开发者_如何学运维teButton.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();
}
});
}
}
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.
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.
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.
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();
}
});
}
}
精彩评论