Android ListView没有填充片段

时间:2015-12-12 17:10:46

标签: java android android-fragments parse-platform

我正在尝试查询类中每个条目的解析,并将其添加到数组列表以显示在ListView上。我试图在一个片段上做这个。但我没有在列表中显示任何项目,在日志中我可以看到已检索到4个项目是正确的,但它们没有显示。 我使用以下代码的任何建议:

 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);



}

}

4 个答案:

答案 0 :(得分:1)

实际上你需要返回rootView, 你正在再次膨胀视图并将其返回,因此它不可见。

public View onCreateView(LayoutInflater inflater,ViewGroup容器,                          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;

}

答案 1 :(得分:0)

您正试图在ListView上展示可能尚未充气的内容。如果对Parse的通话速度快于通货膨胀,您可能会看到NullPointerException。将Parse代码移至onViewCreated()

此外,您将空List传递给ArrayAdapter,然后您将项目添加到List。您需要致电notifyDatasetChanged()

答案 2 :(得分:0)

为什么再次膨胀视图只需在oncreate方法中返回rootVIew。您也可以实例化适配器

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);
});

在oncreate方法中实例化处理程序。

handler = new Handler()

答案 3 :(得分:-1)

尝试在主线程上设置适配器。

  getActivity().runOnUiThread (new Runnable .... etc )

做一个notifyDataSetChanged()也可以工作。