我试图将一些信息从我的片段传递到我的适配器以显示一个ListView,它确实通过但是ListView在完成加载后没有显示任何内容。
我确信适配器类存在问题。
(item
是我通过的数据的自定义类)
适配器代码:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
private final Context context;
private final List<item> markers;
private final static String[] a={"s"};
public MobileArrayAdapter(Context context, List<item> markers) {
super(context, R.layout.list_item);
this.context = context;
this.markers = markers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
TextView views = (TextView) rowView.findViewById(R.id.views);
TextView likes=(TextView) rowView.findViewById(R.id.likes);
TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
TextView desc=(TextView) rowView.findViewById(R.id.desc);
TextView pub =(TextView) rowView.findViewById(R.id.pub);
TextView idnum =(TextView) rowView.findViewById(R.id.idnum);
//Log.e("title",items.get(1).get(0));
title.setText(" "+markers.get(position).getTitle());
// views.setText(items.get(6).get(position));
// likes.setText(items.get(7).get(position));
// upvote.setText(items.get(8).get(position));
// downvote.setText(items.get(9).get(position));
// desc.setText(items.get(4).get(position));
// pub.setText(items.get(5).get(position));
// idnum.setText(items.get(0).get(position));
return rowView;
}
}
logcat中没有错误。
这里有什么问题?
答案 0 :(得分:2)
您必须将数据集,List<item>
传递给ArrayAdapter的超级构造函数或覆盖getCount并返回markers.size()
。
可能是super(context, R.layout.list_item, markers);
或
public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
private final Context context;
private final List<item> markers;
private final static String[] a={"s"};
public MobileArrayAdapter(Context context, List<item> markers) {
super(context, R.layout.list_item);
this.context = context;
this.markers = markers;
}
@Override
public int getCount() {
return (this.markers != null) ? this.markers.size() : 0;
}
// the rest of your code
}
我还建议您对getView
方法进行三次优化:
答案 1 :(得分:1)
这样做
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
private final Context context;
private final List<item> markers;
private final static String[] a={"s"};
public MobileArrayAdapter(Context context, List<item> markers) {
super(context, R.layout.list_item);
this.context = context;
this.markers = markers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
TextView views = (TextView) rowView.findViewById(R.id.views);
TextView likes=(TextView) rowView.findViewById(R.id.likes);
TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
TextView desc=(TextView) rowView.findViewById(R.id.desc);
TextView pub =(TextView) rowView.findViewById(R.id.pub);
TextView idnum =(TextView) rowView.findViewById(R.id.idnum);
//Log.e("title",items.get(1).get(0));
title.setText(" "+markers.get(position).getTitle());
// views.setText(items.get(6).get(position));
// likes.setText(items.get(7).get(position));
// upvote.setText(items.get(8).get(position));
// downvote.setText(items.get(9).get(position));
// desc.setText(items.get(4).get(position));
// pub.setText(items.get(5).get(position));
// idnum.setText(items.get(0).get(position));
return rowView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return markers.size();
}
and then in your activity set the adapter as list.adapter();
}
答案 2 :(得分:0)
如果这有助于其他人......
我有同样的问题(空白ListView项目),在我的情况下,它是由列表行布局的错误布局宽度规范引起的。
不要使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
...
改为使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_height="wrap_content"
android:layout_width="fill_parent"
...
这只发生在我将父母传递给LayoutInflater时(正如我所说的那样),这使得诊断变得令人沮丧。