Senthamil Thendral S
Senthamil Thendral S

Reputation: 508

Wrong row deleted from listview android

I know there are many Stack Overflow questions resolved for my issue. I tried most of the solutions, but none succeeded for me.

I am trying to implement the listrow deletion of listview.On longclick of listrow alert will popup where there are two option delete and cancel.When delete is pressed that row will be deleted in custom adapter as well as row in mysql table using async task. But only the last row gets deleted.

I have set notifyDataSetChanged() to my adapter. Even then it is not working fine.

This is my code:

     listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            System.out.println("postition value::" + position);
            removeItemFromList(position);

            return true;
        }
    });


    protected void removeItemFromList(int position) {
    final int deletePosition = position;
    System.out.println("deleting postition::"+deletePosition);
    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

    alert.setTitle("Delete");
    alert.setMessage("Do you want delete this item?");
    alert.setPositiveButton("YES", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TOD O Auto-generated method stub

            // main code on after clicking yes
            new removefromfav().execute();
            courselist.remove(deletePosition);
        //  dataAdapter.remove(dataAdapter.getItem(deletePosition));
            dataAdapter.notifyDataSetChanged();
            listView.setAdapter(dataAdapter);
        //  dataAdapter.notifyDataSetInvalidated();

        }
    });
    alert.setNegativeButton("CANCEL", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    alert.show();

}

My Adapter Class,

   private class MyCustomAdapter extends ArrayAdapter<Course> {

    private ArrayList<Course> countryList;

    public MyCustomAdapter(Context context, int textViewResourceId,
            ArrayList<Course> countryList) {
        super(context, textViewResourceId, countryList);
        this.countryList = new ArrayList<Course>();
        this.countryList.addAll(countryList);
    }

    private class ViewHolder {
        TextView code;
        TextView name;
        ImageView cover;
        TextView cost;
        ImageView ratingshow;
        ImageView promoimage;
        TextView enroll;
    }

    public void add(Course country) {

        this.countryList.add(country);
    }

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

        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getActivity()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.course_overview, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView
                    .findViewById(R.id.coursename);
            holder.name = (TextView) convertView.findViewById(R.id.author);
            holder.cost = (TextView) convertView.findViewById(R.id.cost);
            holder.cover = (ImageView) convertView.findViewById(R.id.cover);
            holder.ratingshow = (ImageView) convertView
                    .findViewById(R.id.ratingimage);
            holder.promoimage = (ImageView) convertView
                    .findViewById(R.id.promoimage);
            holder.enroll = (TextView) convertView
                    .findViewById(R.id.enroll);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Course country = this.countryList.get(position);
        holder.code.setText(country.getCode());
        holder.name.setText(country.getName());
        holder.cost.setText("$ " + country.getRegion());
        holder.cover.setImageBitmap(country.getBitmap());




        return convertView;

    }

}

Anyone can help me???

Upvotes: 2

Views: 958

Answers (2)

Naveen Kumar Kuppan
Naveen Kumar Kuppan

Reputation: 1442

Ok sure i can help you you will add one more function on your custom adapter

   public void removeElementAtPosition(int position){
      this.countryList.remove(position);
      notifyDataSetChanged();
   }

and delete your adapter item in your long click function like

  dataAdapter.removeElementAtPosition(position);

you don't want to update our adapter once again. And you will check and avoid the null pointer exception

All the adapters will work with this function

 public int getCount(){
     this.countryList.size();         
 }

so, you will affect the list in this adapter your list item will decreased if you check with this getcount function BASE ADAPTER

http://developer.android.com/reference/android/widget/BaseAdapter.html

All the best..

Upvotes: 0

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

You are not deleting anything from your countryList in MyCustomAdapter. I see a function where you add stuff, but I don't see one removing stuff.

Add a function to remove items from your countryList:

private class MyCustomAdapter extends ArrayAdapter<Course> {

private ArrayList<Course> countryList;

public MyCustomAdapter(Context context, int textViewResourceId,
        ArrayList<Course> countryList) {
    super(context, textViewResourceId, countryList);
    this.countryList = new ArrayList<Course>();
    this.countryList.addAll(countryList);
}

private class ViewHolder {
    TextView code;
    TextView name;
    ImageView cover;
    TextView cost;
    ImageView ratingshow;
    ImageView promoimage;
    TextView enroll;
}

public void add(Course country) {

    this.countryList.add(country);
}

//ADD THIS FUNCTION
public void remove(int index) {
    this.countryList.remove(index)
}

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

    ViewHolder holder = null;

    if (convertView == null) {

        LayoutInflater vi = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.course_overview, null);

        holder = new ViewHolder();
        holder.code = (TextView) convertView
                .findViewById(R.id.coursename);
        holder.name = (TextView) convertView.findViewById(R.id.author);
        holder.cost = (TextView) convertView.findViewById(R.id.cost);
        holder.cover = (ImageView) convertView.findViewById(R.id.cover);
        holder.ratingshow = (ImageView) convertView
                .findViewById(R.id.ratingimage);
        holder.promoimage = (ImageView) convertView
                .findViewById(R.id.promoimage);
        holder.enroll = (TextView) convertView
                .findViewById(R.id.enroll);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Course country = this.countryList.get(position);
    holder.code.setText(country.getCode());
    holder.name.setText(country.getName());
    holder.cost.setText("$ " + country.getRegion());
    holder.cover.setImageBitmap(country.getBitmap());




    return convertView;

}
}

And call this in the onClick callback of your dialog

public void onClick(DialogInterface dialog, int which) {
        // TOD O Auto-generated method stub

        // main code on after clicking yes
        new removefromfav().execute();
        dataAdapter.remove(which);
        dataAdapter.notifyDataSetChanged();

    }

Upvotes: 1

Related Questions