【发布时间】:2016-03-11 15:54:55
【问题描述】:
您好 StackOverflow 社区,
我遇到了一个问题(可能是愚蠢的问题),我无法弄清楚我的 ListView 出了什么问题。我有一个自定义适配器,我可以将数据传递给它,但它没有显示任何内容。这是我的代码:
LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.nutrition_main, null);
listview = (ListView) view.findViewById(R.id.listView);
// Pass the results into an ArrayAdapter
ArrayAdapter adapter = new ArrayAdapter<>(getActivity(),
R.layout.listviewtextview);
for (ParseObject calories : ob) {
Log.i("Calories", calories.get("Calories").toString());
adapter.add(calories.get("Calories") + "");
}
ArrayList<String> kapow = new ArrayList<String>();
for(int i = 0; i < adapter.getCount(); i++){
String str = (String)adapter.getItem(i);
kapow.add(str);
Log.i("str", str);
}
SomeAdapter eh = new SomeAdapter(getActivity(), kapow);
listview.setAdapter(eh);
eh.notifyDataSetChanged();
这是我的 SomeAdapter 类代码:
public class SomeAdapter extends ArrayAdapter<String> {
private Context mContext;
private ArrayList<String> mItem;
public SomeAdapter(Context context, ArrayList<String> itemsArrayList) {
super(context, R.layout.listviewtextview, itemsArrayList);
this.mContext = context;
this.mItem = itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.listviewtextview, parent, false);
TextView mFoodName = (TextView) v.findViewById(R.id.food_name);v.findViewById(R.id.food_description);
mFoodName.setText(mItem.get(position) + "");
return v;
}
}
最后但同样重要的是,这是我的 listviewtextview.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/row_activated"
android:layout_height="88dp">
<LinearLayout
android:layout_height="88dp"
android:layout_width="match_parent"
android:layout_marginLeft="16dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/food_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/text_color"
android:textSize="16sp"/>
<TextView
android:id="@+id/food_brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/food_name"
android:singleLine="true"
android:textColor="@color/text_color"
android:textSize="14sp"/>
<TextView
android:id="@+id/food_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:layout_below="@+id/label"
android:singleLine="true"
android:textColor="@color/text_color"
android:textSize="12sp"/>
</LinearLayout>
【问题讨论】:
-
为什么
listview.setAdapter(eh);和eh.notifyDataSetChanged();一起使用? -
您的布局是否在活动中正确膨胀?
-
执行
Log.i("str", str);行? -
确保
ob包含数据,并且您已将膨胀视图添加为可见视图的子视图 -
@Rohodude: 不确定只是尝试覆盖适配器中的
getCount()方法并返回itemsArrayList.size()
标签: java android listview android-listview