Search code examples
androidparse-platformandroid-arrayadapterandroid-listfragment

Populating ParseObject in a ListFragment using custom adapter


I am trying to populate a ListFragment, using a custom ArrayAdapter and a custom Data Object. The persistent remote store is a Parse data set which maps to the custom Data object I've created. The problem is that I cannot populate the queried response from Parse in the array list that is fed to the arrayadapter. When I do this with dummy data, it works fine. When I do with the List, it doesn't.

Would appreciate any help on this.

Snippets:

private List<DataItem> itemsList; //Initialize a list of Data Items type

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    itemsList = new ArrayList<DataItem>();

    //Parse Initialised here

    ParseQuery<ParseObject> query = ParseQuery.getQuery("object");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objectList, ParseException e) {
            if(e == null){
                addItem(objectList);
            } else {
                Log.d("List Activity",e.getMessage());
            }
        }
    });

    //Static Data Population - This works fine
    DataItem item1 = new DataItem();
    item.setName("SampleName");
    item.setPhone("12345678");
    item.setAddress("Mumbai");

    //Set Adapter
    ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
    setListAdapter(adapter);
    adapter.notifyDataSetChanged();
}

public void addItem(List<ParseObject> objectList){
    for(int i =0; i< objectList.size(); i++){
        DataItem item = new DataItem();
        item.setName(objectList.get(i).getString("name").toString());
        item.setPhone(objectList.get(i).getString("phone").toString());
        item.setAddress(objectList.get(i).getString("address").toString());
        itemsList.add(item); //This is not working
    }

}

Solution

  • you need a flat handler for a JSON object between your 'done' and 'addItem'...

    reason is that the query returns a flat string containing JsonRoot object -> containing the the 'results' array that you have to explicitly unwrap prior to calling a method that will create a Map object from a entry in the Json.Results.Array....

    Add something that gets a flat, Json object and iterates the results.array in it...

    public  void getList(JsonNode root){
            String _mimeTyp;
            ArrayNode array = (ArrayNode) root.path("results");
            for(JsonNode node :  array){
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", node.path("objectId").getTextValue());
                map.put("msg", node.path("msg").getTextValue()); 
       ...
                $myArray<HashMap>.add(map);
    

    Then feed that last arrary to a lazyAdapter....

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(myact, $myArray, new AQuery(myact));
    list.setAdapter(adapter);