Question

I have a app with a WishListActivity, but for some reason, when I click on the remove button, it only removes the one last on the list.

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

}

This is my 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;
}

}

Was it helpful?

Solution

I think WishListAdapter.this.notifyDataSetChanged(); is problem thing.
You should using Handler, WishListActivity.java file in define handler.
If remove button click, send message to WhishListActivity's handler.
handler call refresh().
And then, list is refresh change data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top