How to Refresh Android List Adapter, so it shows new-added items

后端 未结 4 1668
一向
一向 2020-12-20 15:51

I was working on a project. It\'s just showing a list of tasks and Adding new tasks to it. I have 3 CLASSES. One for adding, One for view and One to hold all information (or

相关标签:
4条回答
  • 2020-12-20 15:58

    You can use notifyDataSetChanged() method to update your listview.In your case you can use.

    aa.notifyDataSetChanged();
    

    Please Try this way....

    public class MainActivity extends Activity {
    
    ListView lView;
    Button btnAdd;  
    
    private ListView mainListView ;  
    private ArrayAdapter<String> listAdapter ;  
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);          
    
        mainListView = (ListView) findViewById( R.id.listView1 ); 
        btnAdd = (Button)findViewById(R.id.button1);
    
        // Create and populate a List of alphabet names.  
        String[] alphabets = new String[] { "A", "B", "C", "D",  
                                          "E", "F", "G", "H"};    
        ArrayList<String> alphabetsList= new ArrayList<String>();  
        alphabetsList.addAll( Arrays.asList(alphabets) );  
    
        // Create ArrayAdapter using the for the list.  
        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alphabetsList);  
    
    
        // Set the ArrayAdapter as the ListView's adapter.  
        mainListView.setAdapter( listAdapter );      
        btnAdd.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    
    // Here I am adding more alphabets in the list on the listener of add button.
                listAdapter.add( "I" );  
                listAdapter.add( "J" );  
                listAdapter.add( "K" );  
                listAdapter.add( "L" );  
                listAdapter.add( "M" );  
            }
        });       
       }
     }
    
    0 讨论(0)
  • 2020-12-20 16:16

    The solutions proposed thus far work, but are dependent on the Android version of your device. For example, to use the AddAll method you have to put android:minSdkVersion="10" in your android device.

    To solve this questions for all devices I have created my on own method in my adapter and use inside the add and remove method inherits from ArrayAdapter that update you data without problems.

    My Code: Using my own data class RaceResult, you use your own data model.

    ResultGpRowAdapter.java

    public class ResultGpRowAdapter extends ArrayAdapter<RaceResult> {
    
        Context context;
        int resource;
        List<RaceResult> data=null;
    
            public ResultGpRowAdapter(Context context, int resource, List<RaceResult> objects)           {
            super(context, resource, objects);
    
            this.context = context;
            this.resource = resource;
            this.data = objects;
        }
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ........
            }
    
            //my own method to populate data           
            public void myAddAll(List<RaceResult> items) {
    
            for (RaceResult item:items){
                super.add(item);
            }
        }
    

    ResultsGp.java

    public class ResultsGp extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        ...........
        ...........
        ListView list = (ListView)findViewById(R.id.resultsGpList); 
    
        ResultGpRowAdapter adapter = new ResultGpRowAdapter(this,  R.layout.activity_result_gp_row, new ArrayList<RaceResult>()); //Empty data
    
       list.setAdapter(adapter);
    
       .... 
       ....
       ....
       //LOAD a ArrayList<RaceResult> with data
    
       ArrayList<RaceResult> data = new ArrayList<RaceResult>();
       data.add(new RaceResult(....));
       data.add(new RaceResult(....));
       .......
    
       adapter.myAddAll(data); //Your list will be udpdated!!!
    
    0 讨论(0)
  • 2020-12-20 16:16

    You must use every time when data changed.

    aa.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-12-20 16:16

    extends BaseAdapter class ,Override notifyDataSetChanged(); method.
    Please see below snip code:

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            list.addAll();//--insert object to list
        }
    

    call notifyDataSetChanged(); method in Activity onResume() method ,like this:

    @Override
    protected void onResume() {
       super.onResume();
       ((BaseAdapter)getListAdapter()).notifyDataSetChanged();
    }
    
    0 讨论(0)
提交回复
热议问题