-1

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?

Community
  • 1
  • 1
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • Refer my solution for fetching the contact from contact list... http://stackoverflow.com/a/35077124/3946958 – Ravindra Kushwaha Jan 30 '16 at 06:27
  • @RavindraKushwaha Thanks, but that is not what I'm trying to do. I have specific contacts in my arraylist, that the user has already picked using an intent. I want to only keep the contacts from **my** arraylist into the listview. How to do it? – Ruchir Baronia Jan 30 '16 at 06:29

5 Answers5

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.

thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61
0

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

listViewAdapater.notifyDataSetChanged();

hope, this would work

Joseph Joseph
  • 859
  • 2
  • 8
  • 13
0

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.


capt.swag
  • 10,335
  • 2
  • 41
  • 41
  • Thanks, what is `simple_list_item_1`? – Ruchir Baronia Jan 30 '16 at 16:48
  • That is android defaults for spinner layout – capt.swag Jan 30 '16 at 16:49
  • Wait, what spinner layout? Now I'm really confused, I thought it was just my listview I was changing? – Ruchir Baronia Jan 30 '16 at 16:54
  • Sorry, I got it wrong, what I meant is ListView layout. Spinner also got something similar. – capt.swag Jan 30 '16 at 16:56
  • So just to get things clear, I don't modify the actual adapter, I only modify the arraylist. Then, I call notifydatasetchanged to modify the layout that the adapter corresponds to? – Ruchir Baronia Jan 30 '16 at 17:03
  • Yup. like you have a arraylist with 5 names, and the listview is showing those 5, now you add one more to your arraylist, then call notifyDataSetChanged() to see the ListView update to show 6 items. – capt.swag Jan 30 '16 at 17:05
  • Okay, thats exactly what I want! I'm a little confused from Anshul's answer (the one below yours). Do I need to extend a class or something? Thanks! – Ruchir Baronia Jan 30 '16 at 18:18
  • Nope, you don't. If you are extending from a class you will get finer control over your items. But in your case, I don't think it is required – capt.swag Jan 31 '16 at 10:55
0

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

Drenyl
  • 906
  • 1
  • 18
  • 41
0

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();
    }
}
AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • Ohh, that makes sense. Can you provide an example, I am still confused on the adapter part. Thanks! – Ruchir Baronia Jan 30 '16 at 16:50
  • I've updated the post, please check it out ... You call `setData` once you have the new list of items you want to display and it will refresh the list. :) – AndreiBogdan Jan 30 '16 at 17:38