开发者

Update textview in gallery

开发者 https://www.devze.com 2023-03-25 04:06 出处:网络
I am trying to update the textview in my gallery after it has been created using my updateCaption method, however, only the first textview in the gallery gets updated.When I print out my textview, the

I am trying to update the textview in my gallery after it has been created using my updateCaption method, however, only the first textview in the gallery gets updated. When I print out my textview, they all have the same ID like 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{
        publi开发者_StackOverflowc 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;
    }
}

Does anyone know what I am doing wrong?

Added: how I am using updateCaption

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


I think this is due to this line...

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

once all convertView is created, your caption will always point to the textview of the last convertView.

0

精彩评论

暂无评论...
验证码 换一张
取 消