0

I have ListView in which I am setting ImageView,2 TextView and 2 ImageButton. ImageButtons are for add and remove listItem.
Here is my code...

Helper.java

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
.
.
.
import com.example.customObject.ContactBean;
import com.example.helper.HelperAdaptor;
import com.example.helper.ImageHelper;

public class Helper extends Activity implements OnItemClickListener {
    private List<ContactBean> list = new ArrayList<ContactBean>();
    private ListView listView;
    private Button add_helper;
    public HelperAdaptor objAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helper);

        listView = (ListView) findViewById(R.id.lstview_helper_helperlist);
        listView.setOnItemClickListener(this);
        add_helper = (Button) findViewById(R.id.btn_helper_addhelper);
        add_helper.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        ContactListActivity.class);
                startActivityForResult(intent, SELECT_CONTACT);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_CONTACT) {

                ContactBean objContact = new ContactBean();
                objContact.setName(helperName);
                objContact.setPhoneNo(helperNumber);
                objContact.setPic(image);
                list.add(objContact);
                objAdapter = new HelperAdaptor(Helper.this,
                        R.layout.activity_helper_rows, list);
                listView.setAdapter(objAdapter);

            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> listview, View v, int position,
            long id) {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.helper, menu);
        return true;
    }
}

HelperAdapter.java

import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
.
.
.
import com.example.customObject.ContactBean;
import com.example.helpmerestart.ContactListActivity;
import com.example.helpmerestart.R;

public class HelperAdaptor extends ArrayAdapter<ContactBean> {

    private Activity activity;
    private List<ContactBean> items;
    private int row;
    private ContactBean objBean;
    private ViewHolder holder;
    private int itemIndex;
    private View view;

    public HelperAdaptor(Activity act, int row, List<ContactBean> items) {
        super(act, row, items);

        this.activity = act;
        this.row = row;
        this.items = items;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        view = convertView;
        itemIndex = position;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(row, null);

            holder = new ViewHolder();
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if ((items == null) || ((position + 1) > items.size()))
            return view;

        objBean = items.get(position);

        holder.add = (ImageButton) view.findViewById(R.id.btn_helper_add);
        holder.remove = (ImageButton) view.findViewById(R.id.btn_helper_remove);

        if (holder.tvname != null && null != objBean.getName()
                && objBean.getName().trim().length() > 0) {
            holder.tvname.setText(Html.fromHtml(objBean.getName()));
        }
        if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
                && objBean.getPhoneNo().trim().length() > 0) {
            holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
        }
        if (holder.profilepic != null && null != objBean.getPic()) {
            holder.profilepic.setImageBitmap(objBean.getPic());
        }

        holder.add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                addHelper();
            }

        });
        holder.remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                items.remove(itemIndex); 
    //trying to call notifyDataSetChanged() here

            }
        }

        );
        return view;
    }

    public void addHelper() {
        Intent intent = new Intent(activity.getApplicationContext(),
                ContactListActivity.class);
        activity.startActivityForResult(intent, SELECT_CONTACT);
        holder.add.setClickable(false);
        holder.remove.setClickable(true);
    }



    public class ViewHolder {
        public TextView tvname, tvPhoneNo;
        public ImageView profilepic;
        public ImageButton add, remove;
    }
}

the problem is I am not able to remove the listItem for which remove button is clicked. you can refer Previous post to understand what exactly I want to have. I am trying to call notdifyDataSetChanged() in onClick() of remove button but not getting how to do that.

Any other method by which I can get my purpose working will be appritiated.
Regards..
Sourabh

Community
  • 1
  • 1
user2376920
  • 242
  • 1
  • 7
  • 19
  • use `objAdapter.remove(objectToRemove);` – Blackbelt Jul 18 '13 at 13:17
  • `ObjAdapter` is in Helper class and `remove.setOnClickListner()` is in HelperAdaptor. I am trying to notify from `remove.setOnClickListner()`. the main problem is to get `objAdaptor` reference in HelperAdaptor class. – user2376920 Jul 18 '13 at 13:23
  • No I do not get it. you have HelperAdaptor, and you want remove an item from the data set when you press on Remove? – Blackbelt Jul 18 '13 at 13:27
  • yes, that what i want. – user2376920 Jul 18 '13 at 13:34
  • 1
    @user2376920 Then, create a method `public void removeFromAdapter(ObjectType objectToRemove) { objAdapter.remove(objectToRemove); }` in `Helper` class and call this method from your `OnClickListener` (which is in `HelperAdaptor`). Pass `Context` when instantiating `HelperAdaptor` and use it as `(Helper(myContext)).removeFromAdapter(objectToRemove)`. – Vikram Jul 18 '13 at 13:36
  • Thanks Vikram!!! It is working now. – user2376920 Jul 18 '13 at 14:59
  • Vikram!! there is one problem. it is deleting the topmost item from list whether I click any remove button. – user2376920 Jul 19 '13 at 12:56
  • finally Its over. I have used `Object toRemove = arrayAdapter.getItem([POSITION]);arrayAdapter.remove(toRemove);` by passing the position value to Helper class and it works. Thanks to [red Mar 31 '11 at 9:18 Octavian Damiean Suggestion](http://stackoverflow.com/questions/5497580/how-to-dynamically-remove-items-from-listview-on-a-button-click) – user2376920 Jul 19 '13 at 13:06
  • I see that your objects are of type `ContactBean`. Its useful to have an overridden `equals(Object)` method – Vikram Jul 19 '13 at 13:21

1 Answers1

1

You using ArrayAdapter a wrong way. Do not keep your own items list (private List items), because ArrayAdapter has it's own items list inside. To add and remove items to listView use ArrayAdapter's add/addAll and remove methods. You should always use ArrayAdapters methods to manage items if you want it to work properly.

If you dont need ArrayAdapter's functionality, extend BaseAdapter.

P.S. Also you have a problem with itemIndex/holder variable. P.P.S. You use Holder wrong way. (findViewByIds executed every getView call)

There are tons of bugs in your code...

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • there could be bugs. I have modified [this](https://www.dropbox.com/s/mm3umgev99cvmbn/Android%20Contact%20ListView.rar) example to make ListView. Can you please give me any good tutorial which I can refer. – user2376920 Jul 18 '13 at 14:39
  • I can't find nice example of custom arrayadapter ) google it. – Leonidos Jul 19 '13 at 08:22