How to update the `ListView` according to `ArrayLi

2020-05-10 07:19发布

问题:

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 cant find much information about Adapter online, so how do I make sure that my listview always has the contents of my Array? (I have been researching this for 4 and a half hours!)

For example, if I have an array that has the contents "apple" and "orange", then my list should also say "apple" and "orange".

If I chose to remove "apple", then my list should say just "orange". Basically, my list should always be the same as my array.

I saw this, but then how would I go about deleting and adding more items to my list?

回答1:

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.



回答2:

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

listViewAdapater.notifyDataSetChanged();

hope, this would work



回答3:

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



回答4:

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.




回答5:

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();
    }
}