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

9
  • use objAdapter.remove(objectToRemove); Commented Jul 18, 2013 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. Commented Jul 18, 2013 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? Commented Jul 18, 2013 at 13:27
  • yes, that what i want. Commented Jul 18, 2013 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). Commented Jul 18, 2013 at 13:36

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

2 Comments

there could be bugs. I have modified this example to make ListView. Can you please give me any good tutorial which I can refer.
I can't find nice example of custom arrayadapter ) google it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.