0

I tried following the recommendations here (How to refresh Android listview?) to update my ListView, but to no avail.

The relevant code is below. Please note that I call runOnUiThread in my MainActivity to refresh the ListView of this (fragment).

protected ArrayAdapter<String> adapter;
protected View view; 
protected ArrayList<String> theList; 
protected ListView listView;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.fragment_1, container, false);

    listView = (ListView) view.findViewById(R.id.listview);

    theList = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  theList);
    listView.setAdapter(adapter);
    if(view != null) { return view;}

    ((ViewGroup) listView.getParent()).removeView(listView);
    container.addView(listView);

    return view;
}

protected void updateListViewWithPath(String resultFromServerRead) {
        //TODO: Somehow split the information..
    theList.add("yo");
    theList.add("hey");
    adapter.clear();
    adapter.addAll(theList);
    listView.invalidateViews();
    adapter.notifyDataSetChanged();
    listView.refreshDrawableState();
}
Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55
  • you can read get more knowledge about listview from http://www.vogella.com/tutorials/AndroidListView/article.html – kablu May 05 '14 at 19:37

1 Answers1

1

You already have the reference to the arraylist list so you dont need to add it again to the listView just simply add the string to the arraylist and notifyDataSetChanged;

example:

    protected void updateListViewWithPath(String resultFromServerRead) {
        //TODO: Somehow split the information..
    theList.add("yo");
    theList.add("hey");
    adapter.notifyDataSetChanged();
}
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63