您好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>
答案 0 :(得分:0)
您似乎对自定义列表视图及其适配器存在某种误解。由于您将listview的适配器设置为eh
,您甚至不需要具有该阵列适配器,因此您不仅创建了数组适配器,但将数据推入其中,然后您逐个从数组适配器获取数据并将其放入kapow?您需要做的就是使用数据和eh
listview.setadapter(eh)
View view = mInflater.inflate(R.layout.nutrition_main, null);
listview = (ListView) view.findViewById(R.id.listView);
// YOU DONT NEED THIS ADAPTER !
// Pass the results into an ArrayAdapter
// ArrayAdapter adapter = new ArrayAdapter<>(getActivity(),
// R.layout.listviewtextview);
// just pass your calories string to the kapow list.
ArrayList<String> kapow = new ArrayList<String>();
for (ParseObject calories : ob) {
Log.i("Calories", calories.get("Calories").toString());
kapow.add(calories.get("Calories") + "");
}
// You dont need this either
/*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();
修改强>
带有查看者的适配器
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) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listviewtextview, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView = (TextView) v.findViewById(R.id.food_name);v.findViewById(R.id.food_description);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convert.getTag();
}
viewHolder.textView.setText(mItem.get(position) + "");
return convertView;
}
private static class ViewHolder {
TextView textView;
}
}
答案 1 :(得分:0)
public class AFrag extends Fragment{
private View view = null;
public static final String TAG = "A";
ListView listview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.afrag, container, false);
listview = (ListView) view.findViewById(R.id.listView);
//Create your arraylist here no need to get it from adapter
// For testing & sample here i took characters loop.
ArrayList<String> kapow = new ArrayList<String>();
for (char y = 'a'; y <= 'z'; y++) {
kapow.add(String.valueOf(y));
}
// for (ParseObject calories : ob) {
// Log.i("Calories", calories.get("Calories").toString());
// ///Fill arraylist direct from the parseobject.
// //no need to add to adapter & from adapter to arraylist
// kapow.add(calories.get("Calories") + "");
// }
SomeAdapter eh = new SomeAdapter(getActivity(), kapow);
listview.setAdapter(eh);
// no need to call notifyDataSetChanged if you your data not changed after setting adapter
// eh.notifyDataSetChanged();
}
return view;
}
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;
}
public class ViewHolder {
public TextView txtFoodName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.listviewtextview, parent, false);
v = new ViewHolder();
v.txtFoodName = (TextView) convertView.findViewById(R.id.food_name);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.txtFoodName.setText(mItem.get(position) + "");
return convertView;
}
}
}
ListView项目布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="88dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="88dp"
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="#FF0000"
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="#FF0000"
android:textSize="14sp" />
<TextView
android:id="@+id/food_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:paddingRight="16dp"
android:singleLine="true"
android:textColor="#FF0000"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
片段主要布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/txtCurrent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onBtnClick"
android:text="Go to B" />
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>
如果你使用片段而不是上面我已发布代码并给出了同样的评论
建议请查看ViewHolder模式以获取listview
您还可以查看以下与Listview相关的答案 https://stackoverflow.com/a/28105884/1140237