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

后端 未结 4 1689
一向
一向 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 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 alphabetsList= new ArrayList();  
        alphabetsList.addAll( Arrays.asList(alphabets) );  
    
        // Create ArrayAdapter using the for the list.  
        listAdapter = new ArrayAdapter(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" );  
            }
        });       
       }
     }
    

提交回复
热议问题