1
votes

I am new to Android and trying to become strong in programming skills.

Here, I have Custom ListView Adapter class where I used to display the listview items such as textview, images and so on..

What I need to do is, I have delete button inside the listview and if i click the delete button, particular row from the listview has to be deleted and need to refresh the remaining row items inside listview.

The code goes here :

public class CustomListViewAdapter1 extends ArrayAdapter<ListView_Model> {
private Activity activity;
List<ListView_Model> books;


public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
    super(activity, resource, books);
    this.activity = activity;

}

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

    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
   // If holder not exist then locate all view from UI file.
    if (convertView == null) {
        // inflate UI from XML file
        convertView = inflater.inflate(R.layout.item_listview, parent, false);
        // get all UI view
        holder = new ViewHolder(convertView);
        // set tag for holder
        convertView.setTag(holder);

    } else {
        // if holder created, get tag from view
        holder = (ViewHolder) convertView.getTag();
    }

    ListView_Model book = getItem(position);

    holder.name.setText(book.getName());
    holder.authorName.setText(book.getAuthorName());
    holder.mobileNo.setText(book.getPhNo());
    holder.imgView_lv_delete_btn2= (ImageView) 
    convertView.findViewById(R.id.listview_delete_btn2);
    holder.imgView_lv_delete_btn2.setTag(position);


    holder.imgView_lv_delete_btn2.setOnClickListener(new 
    View.OnClickListener() {
       @Override
       public void onClick(View v) {
//I have commented this line here which is not working

       /*Integer index = (Integer)v.getTag();
           System.out.println("Index is:"+index);

           books.remove(index.intValue());
           notifyDataSetChanged();*/
 }
   });
return convertView;
}

private static class ViewHolder {
    private ImageView images,imgView_lv_edit_btn,imgView_lv_view_btn1,imgView_lv_delete_btn2;

    private TextView name;
    private TextView authorName,mobileNo;

    public ViewHolder(View v) {
        name = (TextView) v.findViewById(R.id.title);
        images = (ImageView) v.findViewById(R.id.thumbnail);
        authorName = (TextView) v.findViewById(R.id.author);
        mobileNo = (TextView) v.findViewById(R.id.MobileNo);

        imgView_lv_edit_btn = (ImageView) v.findViewById(R.id.listview_edit_btn);
        imgView_lv_view_btn1 = (ImageView) v.findViewById(R.id.listview_view_btn1);
     }
}}

I have commented this line in the above code since it is not working.

          /*Integer index = (Integer)v.getTag();
           System.out.println("Index is:"+index);

           books.remove(index.intValue());
           notifyDataSetChanged();*/

What mistake am I doing here and why this error is throwing here?

The error thrown is:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference at CustomListViewAdapter1$1.onClick(CustomListViewAdapter1.java:67)

3

3 Answers

1
votes

You are not assigning variable books in a constructor.

public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
    super(activity, resource, books);
    this.activity = activity;
    this.books = books; // The missing line
}

Now your commented code should work!

1
votes

You haven't initialized books at all. Do that in the constructor as follows:

public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
super(activity, resource, books);
this.activity = activity;
this.books = books;
}

Also, you already have the book object so you can do:

books.remove(book) instead of using the integer value.

0
votes

First assign book as

List<ListView_Model> books;

public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
    super(activity, resource, books);
    this.books=books;
    this.activity = activity;

}

and in getView method write code as -

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

    //...............
    //...your core....
    //..............

    holder.imgView_lv_delete_btn2.setOnClickListener(new 
    View.OnClickListener() {
       @Override
       public void onClick(View v) {

           books.remove(position);
           notifyDataSetChanged();*/
 }
   });
return convertView;
}

Thanks