Android - ListView项目不会显示对象数据

时间:2012-06-21 22:08:14

标签: android android-listview baseadapter

我使用BaseAdapter并向其传递具有自己的数据集的对象列表。在listview Id的每一行中,如图像,描述等。每个对象。当我使用BaseAdapter时,我现在设置我没有得到任何错误,我在调试器和logcat中反复检查我的对象列表是有效的而不是null。但由于某种原因,列表使用正确数量的对象构建,但没有ImageView,TextView或任何其他任何数据。每行中唯一的东西是我在列表行的xml布局中使用的相同默认数据。它就像对每个View的set方法的调用一样,或者由于某种原因被忽略,或者视图的内容被传递到屏幕上的另一个上下文或者古怪的东西。无论如何继承我的BaseAdapter类:

 class MaItemAdapter extends BaseAdapter{
        List<Ma> mas = null;
        Context cntx = null;
        //LayoutInflater myInflater;
        int itemPosition =0;
        ViewHolder holder;

        public MarkItemAdapter(Activity cntx,List<Ma> mas){
            super();
            this.mas = mas;
            this.cntx = cntx;
            //myInflater = LayoutInflater.from(cntx);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mas.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mas.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) {
            itemPosition = position;
            RelativeLayout rl = null;
            Mark ma = mas.get(itemPosition);
            if (convertView==null) {
                rl = (RelativeLayout) LayoutInflater.from(cntx).inflate(
                        R.layout.ma_row_item, parent, false);
                holder = new ViewHolder();
                try{
                holder.description = (TextView) rl.findViewById(R.id.ma_description);
                holder.distance = (TextView) rl.findViewById(R.id.ma_distance);
                holder.ma_image = (ImageView) rl.findViewById(R.id.ma_image);
                holder.view_ma = (ImageButton) rl.findViewById(R.id.view_ma);
                holder.rate_ma = (RatingBar) rl.findViewById(R.id.rate_ma);
                holder.description.setText(ma.title);
                holder.distance.setText(ma.getDistanceString());
                holder.view_ma.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    //dostuff
                }

            });
            if(ma.textImg!=null &&
                    !ma.textImg.isRecycled()){
            holder.ma_image.setImageBitmap(ma.textImg);
            }else{
                holder.ma_image.setImageResource(R.raw.skyma);
            }}catch(NullPointerException e){}
            }else{
                rl = (RelativeLayout) convertView;
            }


        //add logic for rating bar

            return rl;
        }


    }

//convenience class to create views 
    private class ViewHolder{
        ImageView ma_image;
        TextView description;
        TextView distance;
        ImageButton view_ma;
        RatingBar rate_ma;

        public ViewHolder(){
            ma_image = new ImageView(getApplicationContext());
            description = new TextView(getApplicationContext());
            distance = new TextView(getApplicationContext());
            view_ma = new ImageButton(getApplicationContext());
            rate_ma = new RatingBar(getApplicationContext());
        }
    }

}

我已经完成了一些教程并且它们工作正常但是当我在我自己的应用程序中使用这个设置时它是不行的。一些正确方向的帮助会很棒。我应该提到的另一件事是,如果我按下评级栏,我会看到每四到五行弹出相同的评级。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在你的getView方法中尝试这个...这就是我自定义适配器的设置方式。

itemPosition = position;
View v = convertView;
Mark ma = mas.get(itemPosition);

if (v == null) {
    LayoutInflater inflater = (LayoutInflater) cntx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.ma_row_item, null);
    holder = new ViewHolder();
}

holder.description = (TextView) v.findViewById(R.id.ma_description);
holder.distance = (TextView) v.findViewById(R.id.ma_distance);
holder.ma_image = (ImageView) v.findViewById(R.id.ma_image);
holder.view_ma = (ImageButton) v.findViewById(R.id.view_ma);
holder.rate_ma = (RatingBar) v.findViewById(R.id.rate_ma);
holder.description.setText(ma.title);
holder.distance.setText(ma.getDistanceString());
holder.view_ma.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        //dostuff
    }

});

if(ma.textImg!=null && !ma.textImg.isRecycled()) {
    holder.ma_image.setImageBitmap(ma.textImg);
} else {
    holder.ma_image.setImageResource(R.raw.skyma);
}

如果这不起作用,我还建议您只在getView方法体内而不是在ViewHolder中初始化您的个人视图。类似的东西:

TextView description = (TextView) v.findViewById(R.id.ma_description);
description.setText(ma.title);