Android ListView删除项目不起作用
我有一个应用程序具有WishListActivity,但由于某种原因,当我单击remove按钮时,它只会删除列表中最后一个 这是我的愿望:Android ListView删除项目不起作用,android,listview,listadapter,Android,Listview,Listadapter,我有一个应用程序具有WishListActivity,但由于某种原因,当我单击remove按钮时,它只会删除列表中最后一个 这是我的愿望: public class WishListActivity extends ListActivity { static ArrayList<String> list; ArrayAdapter<String> arrayAdapter; @Override public void onCreate(Bundle savedInst
public class WishListActivity extends ListActivity {
static ArrayList<String> list;
ArrayAdapter<String> arrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creates a new ArrayLists and populates it
SharedPreferences prefs = WishListActivity.this.getSharedPreferences("com.ben.sizeit", Context.MODE_PRIVATE);
ArrayList<String> sampleList = new ArrayList<String>();
sampleList.add("Sample Item 1");
sampleList.add("Sample Item 2");
Set<String> sampleSet = new HashSet<String>();
sampleSet.addAll(sampleList);
Set<String> set = prefs.getStringSet("wishList", sampleSet);
list = new ArrayList<String>(set);
// Create The Adapter with passing ArrayList as 3rd parameter
arrayAdapter = new WishListAdapter(this, list);
// Sets The Adapter
setListAdapter(arrayAdapter);
// Allows the user to access the home activity
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wish_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.addItem:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(R.string.newItem);
alert.setMessage(R.string.newItemText);
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
list.add(value);
refresh();
// saves the list
SharedPreferences prefs = WishListActivity.this.getSharedPreferences("com.ben.sizeit", Context.MODE_PRIVATE);
Set<String> set = new HashSet<String>();
set.addAll(list);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet("wishList", set);
editor.commit();
}
});
alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled, do nothing
}
});
alert.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void refresh() {
arrayAdapter.notifyDataSetChanged();
}
公共类WishListActivity扩展了ListActivity{
静态数组列表;
ArrayAdapter ArrayAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//创建新的ArrayList并填充它
SharedReferences prefs=WishListActivity.this.getSharedReferences(“com.ben.sizeit”,Context.MODE\u PRIVATE);
ArrayList sampleList=新的ArrayList();
样本列表。添加(“样本项目1”);
样本列表。添加(“样本项目2”);
Set sampleSet=new HashSet();
sampleSet.addAll(sampleList);
Set Set=prefs.getStringSet(“愿望列表”,sampleSet);
列表=新阵列列表(集合);
//使用传递的ArrayList作为第三个参数创建适配器
arrayAdapter=新的WishListAdapter(此,列表);
//设置适配器
setListAdapter(arrayAdapter);
//允许用户访问“主页”活动
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.wish_列表,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.action\u设置:
返回true;
案例android.R.id.home:
NavUtils.navigateUpFromSameTask(本);
返回true;
案例R.id.addItem:
AlertDialog.Builder alert=新建AlertDialog.Builder(此);
alert.setTitle(R.string.newItem);
alert.setMessage(R.string.newItemText);
//设置EditText视图以获取用户输入
最终编辑文本输入=新编辑文本(本);
alert.setView(输入);
alert.setPositiveButton(R.string.ok,新建DialogInterface.OnClickListener()){
public void onClick(对话框接口对话框,int whichButton){
字符串值=input.getText().toString();
列表。添加(值);
刷新();
//保存列表
SharedReferences prefs=WishListActivity.this.getSharedReferences(“com.ben.sizeit”,Context.MODE\u PRIVATE);
Set=newhashset();
set.addAll(列表);
SharedReferences.Editor=prefs.edit();
编辑器.putStringSet(“愿望列表”,set);
commit();
}
});
alert.setNegativeButton(R.string.cancel,新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
//取消,什么也不做
}
});
alert.show();
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
私有无效刷新(){
arrayAdapter.notifyDataSetChanged();
}
}
这是我的愿望:
public class WishListAdapter extends ArrayAdapter<String> {
private final ArrayList<String> list;
private final Activity context;
private ViewHolder viewHolder;
public WishListAdapter(Activity context, ArrayList<String> list) {
super(context, R.layout.list_view_row_item, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.list_view_row_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.itemLabel);
viewHolder.button = (Button) view.findViewById(R.id.removeButton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewParent parentView = v.getParent();
View parent = (View) parentView;
Log.e("DEBUG", parent.toString());
TextView itemLabelTextView = (TextView) parent.findViewById(R.id.itemLabel);
list.remove(list.indexOf(itemLabelTextView.getText().toString()));
WishListAdapter.this.notifyDataSetChanged();
// saves the list
SharedPreferences prefs = context.getSharedPreferences("com.ben.sizeit", Context.MODE_PRIVATE);
Set<String> set = new HashSet<String>();
set.addAll(list);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet("wishList", set);
editor.commit();
}
});
} else {
view = convertView;
}
viewHolder.text.setText(list.get(position));
return view;
}
static class ViewHolder {
protected TextView text;
protected Button button;
}
公共类WishListAdapter扩展了ArrayAdapter{
私人最终阵列列表;
私人最终活动背景;
私人持票人;
public WishListAdapter(活动上下文、ArrayList列表){
super(上下文,R.layout.list\u视图\u行\u项,列表);
this.context=上下文;
this.list=列表;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=空;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
视图=充气机。充气(R.layout.list\u view\u row\u item,空);
viewHolder=新的viewHolder();
viewHolder.text=(TextView)view.findViewById(R.id.itemLabel);
viewHolder.button=(button)view.findViewById(R.id.removeButton);
viewHolder.button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ViewParent parentView=v.getParent();
视图父视图=(视图)父视图;
Log.e(“DEBUG”,parent.toString());
TextView itemLabelTextView=(TextView)parent.findViewById(R.id.itemLabel);
list.remove(list.indexOf(itemLabelTextView.getText().toString());
WishListAdapter.this.notifyDataSetChanged();
//保存列表
SharedReferences prefs=context.getSharedReferences(“com.ben.sizeit”,context.MODE\u PRIVATE);
Set=newhashset();
set.addAll(列表);
SharedReferences.Editor=prefs.edit();
编辑器.putStringSet(“愿望列表”,set);
commit();
}
});
}否则{
视图=转换视图;
}
viewHolder.text.setText(list.get(position));
返回视图;
}
静态类视窗夹{
受保护的文本查看文本;
保护按钮;
}
}我认为
WishListAdapter.this.notifyDataSetChanged()代码>是个问题。
您应该在define Handler中使用Handler,WishListActivity.java文件。
如果单击“删除”按钮,则向WhishListActivity的处理程序发送消息。
处理程序调用刷新()。
然后,列表是刷新更改数据。当我单击按钮时,列表会刷新,但它会删除错误的项。单击按钮时,请检查ArrayList的数据。notifyDataSetChanged是数据更改时的刷新数据。如果删除失败或出错,notifyDataSetChanged方法不起作用。没有错误,但删除方法删除了错误的项。您的意思是删除方法删除了您不想要的项?是的,它总是删除列表底部(最下面)的项。