0

I'm trying to update ListView's data after downloaded JSON data, but it didn't work because onCreate and onCreateView methods are only working at first time. I noticed it when I put debugging point to that two methods but the debugging line never entered there. The response json is correct, but the UI couldn't be updated by parsing ListViewFragment.init(myNewDataList). Here is my ListViewFragment,

public class ListViewFragment extends Fragment{

  ListView listView;

  ArrayList<Incident> incidentLists;

  public static ListViewFragment init(ArrayList<Incident> incidentLists) {
    ListViewFragment listViewFragment = new ListViewFragment();
    listViewFragment.setIncientLists(incidentLists);
    return listViewFragment;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
  }

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

    initUI(view);
    adapter = new ListViewAdapter(getActivity(), incidentLists);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
    return view;
  }

  public ArrayList<Incident> getIncientLists() {
    return incidentLists;
  }

  public void setIncientLists(ArrayList<Incident> incientLists) {
    this.incidentLists = incientLists;
  }

}

And I parse the data to fragment after the JSON downloaded like following snippet, this is AsyncHttpClient's onSuccess method.

@Override
public void onSuccess(int statusCode, Header[] headers,
    String content) {
    try {
        ArrayList<Incident> incidentList = new ArrayList<Incident>();
        JSONObject content_ = new JSONObject(content);
        JSONArray response = content_.getJSONArray("nodes");
        for (int i = 0; i < response.length(); i++) {
            JSONObject nodeObj = response.getJSONObject(i)
                .getJSONObject("node");
            Incident incident = Utils.parseNodeObjectToIncident(nodeObj);
            incidentList.add(incident);
        }
        Fragment fragment = ListViewFragment.init(incidentList);
        Utils.replaceFragment(fragment, MainActivity.this);
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(),"JSON Exception", Toast.LENGTH_LONG).show();
    }
}

I doubt that my ListViewFragment only happen once at the first time because it is confusing with BackStack and transaction stuffs at Utils.replaceFragment().

public static void replaceFragment(Fragment fragment,
            AppCompatActivity activity) {
        String backStateName = fragment.getClass().getName();
        String fragmentTag = backStateName;
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        boolean fragmentPopped = fragmentManager.popBackStackImmediate(
                backStateName, 0);
        if (!fragmentPopped
                && fragmentManager.findFragmentByTag(fragmentTag) == null) {
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, fragment,
                    fragmentTag);
            // fragmentTransaction.add(R.id.frame_container, fragment);
            fragmentTransaction
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.addToBackStack(backStateName);
            // fragmentTransaction.commitAllowingStateLoss();
            fragmentTransaction.commitAllowingStateLoss();
        }
    }

Any Help?

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
  • 1. Why you overide ``OnCreate`` method when use ``Fragment`` 2. in ``success`` function, you just update value of ``incidentLists`` , where you call ``adapter.notifyDataSetChanged();`` ??? – Danh DC Jan 11 '16 at 03:56
  • overiding OnCreate is just for putting debugging point if that's entered there or not. I call adapter.notifyDataSetChanged() at onCreateView. Anything wrong? – Zin Win Htet Jan 11 '16 at 04:07
  • Because, you just call ``adapter.notifyDataSetChanged();`` one time in ``OnCreateView`` method, so listview not update after you change value ``incidentLists``. Try init **Listview** and **adapter** same **incidentLists**, and call function ``adapter.notifyDataSetChanged();`` when you update ``incidentLists`` – Danh DC Jan 11 '16 at 04:16
  • Everytime when we create new Fragments, onCreateView must be effect after init() method. So, adapter.notifyDataSetChanged() method too cuz it's inside onCreateView. Plus, it's impossible to create ListView outside of the onCreateView method I think. – Zin Win Htet Jan 11 '16 at 04:40

1 Answers1

1

Basically your problem is that you are not able to update list when you have got latest set of information from server. I would have solved this problem with the below code:

In list view adapter keep a method as written below:

public class ListFragmentAdapter extends <your adapter extension>{

private Context context;
private List<Incident> incidents;
//constructor
public ListFragmentAdapter (Context context,List<Incident> initialIncidentList){

this.context = context;
this.incidents = initialIncidentList;
}
public void updateListFragment(List<Incident> latestIncidents){
incidents.addAll(latestIncidents);
this.notifyDataSetChanged();
}
}

Comment below if you have any doubts

Manikanta
  • 3,421
  • 4
  • 30
  • 51
  • Thank you. But I solved with another way. I just deleted that **if** condition of replaceFragment() method. It's only ok with static UI fragments. The transaction with updated data never commited because of that condition. That's why onCreateView never called except first time. – Zin Win Htet Jan 11 '16 at 05:14
  • I just copied that method from somewhere of internet but now I realized that it doesn't match with my apps's requirement. – Zin Win Htet Jan 11 '16 at 05:15
  • But replace fragment is a costly affair as you are replacing fragment everytime – Manikanta Jan 11 '16 at 05:26
  • like i've other options. :( – Zin Win Htet Jan 11 '16 at 06:14