0

I am trying to make a list with titles,descriptions and other data using custom listview in every row with delete imagebutton.

When I click on the ImageButton in row number 10 ,row number 5 from ListView is getting deleted!

enter image description here

myadapter :

public  static class ViewHolder {
    TextView favorites_title;
    TextView favorites_description;
    TextView favorites_date;
    ImageButton favorites_btn_delete ;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if(convertView==null){
        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        convertView = inflater.inflate(R.layout.favorites_list_row_layout, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.favorites_title = (TextView) convertView.findViewById(R.id.favorites_title);
        holder.favorites_description = (TextView) convertView.findViewById(R.id.favorites_description);
        holder.favorites_date = (TextView) convertView.findViewById(R.id.favorites_date);
        holder.favorites_btn_delete = (ImageButton)convertView.findViewById(R.id.favorites_btn_delete); 
       /************  Set holder with LayoutInflater ************/
        convertView.setTag( holder );
        holder.favorites_btn_delete.setTag(position);
    }
    else 
        holder=(ViewHolder)convertView.getTag();
    holder.favorites_btn_delete.getTag();
    NewsItem newsItem = (NewsItem) listData.get(position);

    holder.favorites_title.setText(newsItem.getHeadline());
    holder.favorites_description.setText(newsItem.getDescription());
    holder.favorites_date.setText(newsItem.getDate());
    holder.favorites_btn_delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Integer index = (Integer) v.getTag();
            int i = index.intValue();
            Log.e("Position",  String.valueOf(i));
        //  listData.remove(i);
        //  notifyDataSetChanged();

        }
    });
1
  • 1
    already you took convertview.getTag() so just pass the default position what you get from getview. listData.remove(position); Commented Jan 21, 2014 at 7:31

3 Answers 3

1

Move holder.favorites_btn_delete.setTag(position); outside the if else or else the new position values will never be set as tag in the Button when convertView is not null and the old values will remain as tag

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

Comments

0

holder.favorites_btn_delete.setOnClickListener(this);

outside the getview method

public void onClick(View v)
    {
  // TODO Auto-generated method stub
            Integer index = (Integer) v.getTag();
            int i = index.intValue();
            Log.e("Position",  String.valueOf(i));
          listData.remove(i);
        notifyDataSetChanged();

}

Comments

0

use this code..it will help u..

apply onItem click listener on image button

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
         listData.remove(positon);
        notifyDataSetChanged();
    }

Apply this on your activity.

Comments

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.