How to dynamically update a ListView with custom adapter?

核能气质少年 提交于 2019-11-29 00:19:11
oybek.t

Whenever you add new data you should do this:

adapter.notifyDataSetChanged()

For example:

database.insert(data);

myAdapter.notifyDataSetChanged();

OR:

myAdapter.mySetNewContentMethod(someNewContent);
myAdapter.notifyDataSetChanged();
Rajnish Mishra

After updating your adapter call

myAdapter.notifyDataSetChanged();

This will dynamically update the list view.

See this for more info LINK

I have tried the simplest method to update the list when you delete a item from it. Hope so my code would help you. Code

   //Custom Adatpter
   package com.example.smsptclapp;
   import java.util.ArrayList;
   import java.util.List;
   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.ViewGroup;
   import android.widget.AdapterView;
   import android.widget.BaseAdapter;
   import android.widget.ImageView;
   import android.widget.TextView;
   import android.widget.Toast;
   import android.widget.AdapterView.OnItemLongClickListener;

   public class CustomAdapter extends BaseAdapter
    {
 static List<String> result;
 //ArrayList<SingleRow> list;
 Activity context;
 static TextView tv;
 static int k,count;
     // int [] imageId; //for Image
     private static LayoutInflater inflater = null;
    public CustomAdapter(Activity activity, List<String> prgmNameList) 
     {
       // TODO Auto-generated constructor stub
        result = prgmNameList;
        context = activity;
       //imageId = prgmImages; //for Image 
       inflater = ( LayoutInflater )context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
     }

     public CustomAdapter(OnClickListener onClickListener, List<String>  listItems) 
     {
    // TODO Auto-generated constructor stub
     }

 @Override
     public int getCount() 
     {
      // TODO Auto-generated method stub
      return result.size();
     }

     @Override
     public Object getItem(int position) 
     {
       // TODO Auto-generated method stub
       return position;
     }

     @Override
     public long getItemId(int position) 
     {
       // TODO Auto-generated method stub
       return position;
     }

     public static int getCounter()
    {
      return count;
    }

    public static int getPosition()
   {
     return k;
   }

   @Override
   public View getView(final int position, View convertView, ViewGroup   parent) 
   {
     // TODO Auto-generated method stub
     //Object cd1=   getItem(position);
     View rowView;       
     rowView = inflater.inflate(R.layout.program_list, null);
     tv=(TextView) rowView.findViewById(R.id.textView1);
     tv.setLongClickable(true);
     tv.setText(result.get(position));
      rowView.setOnClickListener(new OnClickListener() 
      {
         @Override
         public void onClick(View v) {
            k = position;
            Intent i =  new Intent(context, Menu.class);
            context.startActivity(i);
         }
         });
     return rowView;
    }
 }

 // In Main Type these lines
       ss =listItems.get(i);  // where 'ss' is global string, 'i' is the location you want to delete, and 'listItems' is global variable of 'List<String>' type. 
       db.deleteUser(ss);  //'db' is global variable of database and deleteUser is method to delete entry from data base only 
   listItems.remove(i); //to remove the entry from list i.e to refresh it you must call this
   listgroups.setAdapter(new CustomAdapter(this,listItems)); //'listgroups' is 'ListView', which is also global.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!