这个问题在这里已经有了答案:
setText fails to show a number as text in a TextView
(2 个回答)
6年前关闭。
我正在尝试制作一个简单的 ListView ,其中包含您一整天吃过的不同食物的列表。我创建了一个简单的 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;
}
}
这是包含 ListView 的 fragment 的代码:
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 方法有问题,就像它没有正确充气一样。我错过了什么吗?
最佳答案
FoodItem 的 calorieCount 属性是 int 类型,对吗?在这种情况下,在这一行:
calorieTextView.setText(item.getCalorieCount());
TextView 会认为您正在尝试通过其 int id 引用 String 资源 - 因为 Java 是如何工作的。尝试将该值转换为字符串,例如:
calorieTextView.setText(String.valueOf(item.getCalorieCount()));
关于android - 这个简单的 android listView 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293952/