这个简单的android listView出了什么问题?

时间:2015-05-18 01:37:27

标签: android listview android-listview layout-inflater

我试图制作一个简单的列表视图,其中包含您一整天都吃过的不同食物的列表。我创建了一个简单的foodItem类,它只包含一个字符串和一个卡路里计数器,我创建了一个FoodItemAdapter类,它覆盖了ArrayAdapter类。出于某种原因,当我尝试启动应用程序时崩溃,看起来textViews尚未正确加载。这是错误:

05-17 14:20:20.528  21314-21314/com.example.jake.caloriecounter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jake.caloriecounter, PID: 21314
    android.content.res.Resources$NotFoundException: String resource ID #0x64
            at android.content.res.Resources.getText(Resources.java:286)
            at android.widget.TextView.setText(TextView.java:4148)
            at com.example.jake.caloriecounter.FoodItemAdapter.getView(FoodItemAdapter.java:38)

这是食品项目适配器的代码

public class FoodItemAdapter extends ArrayAdapter<FoodItem> {
    Context context;
    String TAG = "FoodItemAdapter";
    public FoodItemAdapter(Context context, int resourceId, ArrayList<FoodItem> foodList) {
        super(context,resourceId,foodList);
        this.context = context;
    }
    @Override public View getView(int position, View convertView, ViewGroup parent) {
        //Inflate if needed
        if(convertView == null) {
           //Need to create a new view
           Log.d(TAG,"Creating new view.....");
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.layout_food_item, null);
        } else {
            Log.d(TAG,"Using old convertView.....");
        }

        FoodItem item = getItem(position); //Gets data assosciated with this position from the data
        if(item != null) {
            //Get views so we can set them to the correct data
            TextView foodTextView = (TextView) convertView.findViewById(R.id.foodName);
            TextView calorieTextView = (TextView) convertView.findViewById(R.id.calorieCount);
            //Set the views based on the data
            foodTextView.setText(item.getName());
            calorieTextView.setText(item.getCalorieCount());
        }

        return convertView;
    }

}

这是包含列表视图的片段的代码:

public class DayListFragment extends Fragment {
    private static final String TAG = "DayListFragment";
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DayListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DayListFragment newInstance(String param1, String param2) {
        DayListFragment fragment = new DayListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    public DayListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d(TAG,"Inflating....");
        View view = inflater.inflate(R.layout.fragment_day_list, container, false);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG,"Starting....");
        //Setup data source (temporary)
        ArrayList<FoodItem> foodArray = new ArrayList<FoodItem>();
        foodArray.add(new FoodItem("HotDog",100));
        foodArray.add(new FoodItem("Cake",500));
        //Set up the adapter & hook it up to the listView
        FoodItemAdapter foodAdapter = new FoodItemAdapter(getActivity(),R.layout.layout_food_item,foodArray);
        ListView listView = (ListView) getActivity().findViewById(R.id.listViewMain);
        listView.setAdapter(foodAdapter);


        //These few lines work fine on their own, so I know that findViewById works here
        TextView temp = (TextView) getActivity().findViewById(R.id.tempText);
        temp.setText("Test Text");

    }
}

我尝试了一些不同的解决方案,但我还没有把事情搞定。我觉得getView方法存在问题,比如它没有正确膨胀。我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

FoodItem的calorieCount属性是int类型,对吗?就这一行而言:

calorieTextView.setText(item.getCalorieCount());

TextView会认为您正在尝试通过其int id引用String资源 - 因为Java的工作原理。尝试将该值转换为String,例如:

calorieTextView.setText(String.valueOf(item.getCalorieCount()));

答案 1 :(得分:0)

我猜你太快就要创建适配器了。 确保等到onActivityCreated()

您可以在http://developer.android.com/guide/components/fragments.html

了解有关Fragment生命周期的更多信息