0

I have here a baseadapter based on what I saw in the API demos. I want to dynamically add/remove items from the list, in this case adding from an intent called by a button, and removing from clicking on an imageview in the listview. And in this case, I would be adding/removing items from DATA[]. I have looked around for various AddItem() and or Remove() methods on SO and google, but have not really come up with much for this situation. Any help would be great. Here is the code:

public class myActivity extends Activity{
    private static final int CONTACT_PICKER_RESULT = 1001;  
    private static final String TAG = myActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new myAdapter(this));     
        Button bAdd = (Button)findViewById(R.id.bAdd);
        bAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        ........xxxx....xxxx....
        }
public class myAdapter extends BaseAdapter{
        private LayoutInflater mInflater;

        public myAdapter(Context context) {
            mInflater = LayoutInflater.from(context);

        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return DATA.length;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }


        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.row,null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.txtTitle);
                holder.icon = (ImageView) convertView.findViewById(R.id.imgIcon);
                convertView.setTag(holder);


            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(DATA[position]);
            holder.icon.setImageResource(android.R.drawable.ic_delete);

            return convertView;
        }



        }


    static class ViewHolder {
        TextView text;
        ImageView icon;
    }
//this will not be hard-coded, jsut included for clarity
private static final String[] DATA ={
        "one","two","three" };
benbeel
  • 1,532
  • 4
  • 24
  • 38

1 Answers1

1

If all you need to to able to do is add/remove items from your DATA[] array, why not just replace the array with a simple List, implemented by an ArrayList for example.

This would allow you to write click handlers for your adapter which add/remove items from the list as needed.

tomato
  • 3,373
  • 1
  • 25
  • 33
  • Yes! Using an arraylist was definately the way. That combined with this http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences solved it. Thank you! – benbeel Nov 10 '11 at 04:42