更新库中的textview

时间:2011-08-04 20:27:45

标签: android image gallery adapter

我正在尝试使用我的updateCaption方法创建我的库中的textview,但是,只有库中的第一个textview得到更新。当我打印出我的textview时,它们都具有相同的ID,如android.widget.TextView@40942d60。

public class ImageAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private int[] image;
    private TextView caption;

    public ImageAdapter(Context context) {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        image = new int[4];

        for (int i = 0; i < image.length; i++)
        {
              image[i] = R.drawable.image;
        }
    }

    public void updateCaption(String text, int position)
    {
        System.out.println(position + ": text");
        caption.setText(text);

        System.out.println(caption);
    }

    public int getCount() {
        return image.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView text, dssnum, scnum;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View vi = convertView;
        ViewHolder holder;

        if (convertView == null)
        {
            vi = inflater.inflate(R.layout.caption, null);
            holder = new ViewHolder();
            caption = holder.text = (TextView) vi.findViewById(R.id.textView1);
            holder.scnum = (TextView) vi.findViewById(R.id.scnum);
            holder.dssnum = (TextView) vi.findViewById(R.id.dss_num);
            holder.image = (ImageView) vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
            holder = (ViewHolder) vi.getTag();


        holder.dssnum.setText("Unavailable");

        holder.image.setImageResource(image[position]);
        holder.text.setText("Not available.");
        return vi;
    }
}

有谁知道我做错了什么?

添加:我如何使用updateCaption

for (int j = 0; j < listOfList.get(i).size(); j++)
{
        galleryImageAdapter.updateCaption("test " + j, j);
 }

1 个答案:

答案 0 :(得分:1)

我认为这是由于这条线......

caption = holder.text = (TextView) vi.findViewById(R.id.textView1);

创建所有convertView后,您的标题将始终指向最后一个convertView的textview。