【发布时间】:2013-11-23 21:14:51
【问题描述】:
我正在尝试使用基本适配器来使用我的对象 (NewsFeedObj) 中的字段填充我的列表视图我目前只是尝试获取描述字段。当我运行我的代码时,我只是得到一个空白活动?谁能帮帮我
public class News_Feed_BaseAdapter extends BaseAdapter {
private Context context;
private List<NewsFeedObj> obj = Collections.emptyList();
public News_Feed_BaseAdapter(Context context, List<NewsFeedObj> obj) {
this.context = context;
this.obj = obj;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return obj.size();
}
@Override
public NewsFeedObj getItem(int position) {
// TODO Auto-generated method stub
return obj.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.newfeed_layout, parent, false);
}
TextView t = (TextView) convertView.findViewById(R.id.Description);
NewsFeedObj o = getItem(position);
t.setText(o.get_description());
return convertView;
}
}
在主要活动中
ArrayList<NewsFeedObj> NewsObjs = new ArrayList<NewsFeedObj>();
NewsObjs = j.new_feed_Array_List(); // returns an array list of
// NewsFeedObj
ListView l = getListView();
News_Feed_BaseAdapter A = new News_Feed_BaseAdapter(this, NewsObjs);
l.setAdapter(A);
【问题讨论】:
-
你试过调试吗?这将更容易找出问题所在。可能您的 NewsObj 是空的(大小 0)
-
很难用你显示的代码说。在我看来一切都很好。您确定 ListView 本身显示在屏幕上并且您的列表中有项目吗?也许您可以显示更多代码或您的主要布局。只是一个说明:使用默认的 java/android 编码风格。它使您的代码更容易被其他人阅读。使用 camelcase 并在此处检查变量和方法的命名方式:source.android.com/source/code-style.html
-
你得到 o 的值了吗?
-
我试过 Log.d("response ok", "Value of " + t.getText().toString());打印描述的值,但它不打印任何东西
-
getView() 方法似乎没有运行
标签: android listview arraylist baseadapter