Home  >  Forum  >  Android
Post New Query

Android ListView delete item and refresh


joined on
August 12,2015
Asked on August 12,2015
I have ListView in which I need to delete item one by one. How to get row id to delete it and then refresh the ListView.
joined on
January 20,2015
Replied on August 13,2015

  In your BaseAdapter sub class within  getView() method perform the delete operation.

    public View getView(final int position, View view, ViewGroup parent) {
       final Student student = (Student) getItem(position);
        if (view == null) {
            view = inflater.inflate(R.layout.student_report, null);
        }
   //some code
     Button deleteButton = (Button)view.findViewById(R.id.delete_btn);
     deleteButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             
     dbHelper.deleteData(stdId);
     students.remove(position);
     notifyDataSetChanged();
         }
     });
     return view;
   }
       @Override
    public Object getItem(int position) {
        return students.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getCount() {
        return students.size();
    }
    public void setStudents(List<Student> data) {
        students.clear();
        students.addAll(data);
        notifyDataSetChanged();
    }


Write Answer



Sitewide-Feb4of4234x60





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us