当前位置: 代码迷 >> Android >> ListView如何显示不了,Adapter实现是否有有关问题.哪位帮忙看上.
  详细解决方案

ListView如何显示不了,Adapter实现是否有有关问题.哪位帮忙看上.

热度:89   发布时间:2016-05-01 17:29:38.0
求助:ListView怎么显示不了,Adapter实现是否有问题...哪位帮忙看下...
关于这个花了好一个晚上的时间最后还是没有解决,个人觉得应该没有什么问题可是就是有问题..
贴上源码:
Java code
class CommentAdapter extends BaseAdapter{     public  CommentAdapter(){          Log.d("Infor", comment_list.size()+"");               }    @Override    public int getCount() {        // TODO Auto-generated method stub        return comment_list.size();    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return comment_list.get(position);    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public View getView(int position, View arg1, ViewGroup arg2) {        // TODO Auto-generated method stub        LayoutInflater layoutInflater = LayoutInflater.from(Comment.this);//从当前上下文得到LayoutInflater        rowView = rowViews.get(position);        if(rowView!=null){            rowView=layoutInflater.inflate(R.layout.comment_item, null);            TextView comment_name = (TextView) rowView.findViewById(R.id.comment_item_name);            TextView comment_time = (TextView) rowView.findViewById(R.id.comment_item_time);            TextView comment_text = (TextView) rowView.findViewById(R.id.comment_item_text);            comment_name.setText(comment_list.get(position).get("comment_user_name"));                comment_time.setText(comment_list.get(position).get("create_time"));            comment_text.setText(comment_list.get(position).get("comment_text"));                            rowViews.put(position, rowView);                }    return rowView;    }          }


Java code
    CommentAdapter adapter=new CommentAdapter();        comment_listview.setAdapter(adapter);    


其中CommentAdapter是个内部类,comment_list是个列表List<Map>该值验证确认无误的...
可是最后列表根本就没有显示出来,还报空指针异常。我就纳闷了,没有空指针啊,comment_list什么的都获得布局里的对象了的。大家帮忙看下,感谢...

------解决方案--------------------
rowViews是什么东东,没看到实例化啊,而且这个地方为什么要这么写?listView本身就有缓存,为什么好用rowViews来缓存?
------解决方案--------------------
getView里面要改:

if (arg1 == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.comment_item, parent, false);
}

TextView comment_name = (TextView) arg1.findViewById(R.id.comment_item_name);
TextView comment_time = (TextView) arg1.findViewById(R.id.comment_item_time);
TextView comment_text = (TextView) arg1.findViewById(R.id.comment_item_text);
comment_name.setText(comment_list.get(position).get("comment_user_name"));
comment_time.setText(comment_list.get(position).get("create_time"));
comment_text.setText(comment_list.get(position).get("comment_text"));

return arg1;
------解决方案--------------------
楼主你的adapter里面没有list数据的,所以不会显示,在构造方法中,传一个list
------解决方案--------------------
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = LayoutInflater.from(Comment.this);//从当前上下文得到LayoutInflater
if(rowView==null){
rowView=layoutInflater.inflate(R.layout.comment_item, null);
}
//rowView = rowViews.get(position);
// if(rowView!=null){
  相关解决方案