How to update the `ListView` according to `ArrayList`?

前端 未结 5 466
傲寒
傲寒 2021-01-29 08:37

I have an array of contacts that I am constantly updating. I want my ListView to update with my contacts.

Should I use an Adapter for that? I

相关标签:
5条回答
  • 2021-01-29 09:21

    Instead of an Array, I recommend using ArrayList.

    The advantage of ArrayList over Array is that arrays require fixed size. So if you are initializing an Array

    String names[] = new String[10];
    

    the size is 10, and when you set to ListView, the ListView will show 10 lists, even though there is name only in 8 array positions

    Where as if you use ArrayList, you don't have to worry about size, since it can scale up and down

    List<String> names = new ArrayList<>();
    names.add("Raul");
    names.add("Khan");
    names.remove(1);
    

    add() adds an item to the list, while remove() removes an item.

    With respect to your answer, say you have an ArrayList of items

    and when you have a new item to add to the list, just add it to your ArrayList with the add() method, and finally call notifyDataSetChanged() on your ArrayAdapter.

    And YES, you have to use an ArrayAdapter like this

    ArrayList<String> namesList = new ArrayList<>();
    ArrayAdapter arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, namesList);
    

    And when you have changes to the list, ie when you add items to your list, call notifyDataSetChanged() on arrayAdapter.


    0 讨论(0)
  • 2021-01-29 09:26

    Create a ListAdapter something like this.

    public class MyListAdapter extends BaseAdapter {
    
    
      private ArrayList<String> names = new ArrayList();
    
      @Override
      public int getCount() {
        return names.length;
      }
    
      @Override
      public Object getItem(int location) {
        return names[location];
      }
    
      @Override
      public long getItemId(int position) {
        return position;
      }
    
      private static class ViewHolder {
        public TextView listViewNgoName, listViewDistance, listViewRating, listViewAbout, ngoIcon;
      }
    
      public MyListAdapter(
          ArrayList names) {
        this.names = names;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
         // Write code for instantiating a row based view.
        return convertView;
      }
    }
    

    Now in your Activity

        ArrayList<String> names = new ArrayList();
        names.add("oranges");
        names.add("apple");
        ListView listView = (ListView) findViewById(R.id.listview);
        MyListAdapter adapter = new MyListAdapter(names);
        listView.setAdapter(adapter);
    

    Lets suppose you need to update the listview on the button click. ie. on button click the values in your array gets changed and you wanted to update the listView.

    Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            names.add("payaya");
            names.add("grapes");
            adapter.notifyDataSetChanged();
          }
        });
    

    I have not written the entire code. Just a head start for you.

    0 讨论(0)
  • 2021-01-29 09:39

    so, You have to notify your ListView adapter that the data has changed.

    listViewAdapater.notifyDataSetChanged();
    

    hope, this would work

    0 讨论(0)
  • 2021-01-29 09:40

    use ArrayList instead of Array[], you don't need to initialize size for ArrayList so it is more flexible than Array[]. and it will automatically scale up or down if you add or delete items inside

    0 讨论(0)
  • 2021-01-29 09:44

    I usually have a method called setData(List<whatever> data) in my Adapter. My Adapter is let's say in an Activity. When In my activity I obtain an updated array from wherever, I call setData() with the new array containing the updated array.

    In my Adapter I have a field defined like: private List<Whatever> mItems. I use this field in the getCount() method of the Adapter to obtain the list items, basically I use this list to populate the ListView.

    What I do in the setdata() is assign data to mItems and I call notifyDataSetchanged() which refreshes the ListView and this time it will use the newly set data values, so everything updates to the appropriate state.

    UPDATE

    I don't have quick access to an adapter for a ListView but the one below should do, it's for a ViewPager, but the principle is the same:

    public class AdapterTweets extends PagerAdapter {
        private List<Tweet> mItems;
    
        // [...]
    
        @Override
        public int getCount() {
            return mItems == null ? 0 : mItems.size();
        }
    
        // [...]
    
        public void setData(List<Tweet> items) {
            mItems = items;
            notifyDataSetChanged();
        }
    }
    
    0 讨论(0)
提交回复
热议问题