Android : Populating ParseObject in a ListFragment using custom adapter

on Sunday, November 2, 2014


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
}

}

0 comments:

Post a Comment