ListView删除项目不起作用

时间:2014-03-10 00:35:52

标签: android listview listadapter

我有一个带有WishListActivity的应用程序,但出于某种原因,当我点击删除按钮时,它只删除列表中的最后一个。

这是我的愿望清单活动:

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();
}

}

这是我的WishListAdapter:

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;
}

}

1 个答案:

答案 0 :(得分:0)

我认为WishListAdapter.this.notifyDataSetChanged();是问题所在 您应该在define handler中使用Handler,WishListActivity.java文件 如果删除按钮单击,则向WhishListActivity的处理程序发送消息 handler call refresh()。
然后,列表是刷新更改数据。