I am trying to query parse for every entry in a Class and add it to an array list to display on a ListView. I am attempting to do this on a fragment. But I am not getting any items showing in the list, in the log I can see 4 items have been retrieved which is correct, but they are not displaying. Any suggestions I am using the following code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class InventoryDisplayFragment extends Fragment {
ArrayList<String> inventoryItems;
ArrayAdapter arrayAdapter;
public InventoryDisplayFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inventoryItems = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, inventoryItems);
final View rootView = inflater.inflate(R.layout.fragment_inventory_display, container, false);
final ListView productsList = (ListView) rootView.findViewById(R.id.itemlistView);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Inventory");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> stockItemList, ParseException e) {
if(e == null){
if(stockItemList.size() > 0){
for(ParseObject unit : stockItemList){
inventoryItems.add(unit.get("productName").toString());
Log.d("unit", "retrieved: " + stockItemList.size());
}
productsList.setAdapter(arrayAdapter);
}
} else {
Log.d("unit", "Error: " + e.getMessage());
}
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_inventory_display, container, false);
}
}
Actually you need to return the rootView, you are inflating the view again and returning it, so it is not visible.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inventoryItems = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, inventoryItems);
final View rootView = inflater.inflate(R.layout.fragment_inventory_display, container, false);
final ListView productsList = (ListView) rootView.findViewById(R.id.itemlistView);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Inventory");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> stockItemList, ParseException e) {
if(e == null){
if(stockItemList.size() > 0){
for(ParseObject unit : stockItemList){
inventoryItems.add(unit.get("productName").toString());
Log.d("unit", "retrieved: " + stockItemList.size());
}
productsList.setAdapter(arrayAdapter);
}
} else {
Log.d("unit", "Error: " + e.getMessage());
}
}
});
// Inflate the layout for this fragment
return rootview;
}
You are trying to display things on a ListView
that might have not been inflated yet. If the call to Parse
is faster than inflation you will potentially see a NullPointerException
. Move the Parse
code to onViewCreated()
.
Also, you are passing an empty List
to the ArrayAdapter
and then you are adding items to the List
. You need to call notifyDatasetChanged()
.
why are you inflating your view again just return rootVIew in your oncreate method. Also you may instantiate adapter
for(ParseObject unit : stockItemList){
inventoryItems.add(unit.get("productName").toString());
Log.d("unit", "retrieved: " + stockItemList.size());
}
arrayAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, inventoryItems);
handler.postAtFrontofQueue(new Runnable(){
productsList.setAdapter(arrayAdapter);
});
where in instantiating handler in your oncreate method.
handler = new Handler()
Try setting the adapter on main thread .
getActivity().runOnUiThread (new Runnable .... etc )
do a notifyDataSetChanged() can also work .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.