基于getView()方法中的数据设置图像

时间:2012-01-12 07:04:32

标签: android android-listview adapter

IndexGainersLosers是3 buttons。 在每次button点击时,listView会填充相应的数据。

第3列%Chg可以否定正面

根据%Chg值的值,相应的arrow应显示在imageView%Chg之后,{<1}}列值向上箭头 < em>正值和向下箭头 负值 ...

在任何button点击中,我将%Chg列中的updown列的值传递给Adapter

根据该值,Adapter应设置 down arrow up arrow

Adapter类的代码片段(getView()方法)

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_table, null);
            holder = new ViewHolder();

            holder.txtFirst = (TextView) convertView
                    .findViewById(R.id.FirstText);
            holder.txtSecond = (TextView) convertView
                    .findViewById(R.id.SecondText);
            holder.txtThird = (TextView) convertView
                    .findViewById(R.id.ThirdText);
            holder.ivarrow = (ImageView) convertView.findViewById(R.id.ivarrow);
            holder.l1 = (LinearLayout) convertView
                    .findViewById(R.id.tableLayout);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> map = list.get(position);
//Setting the row background
        if (count % 2 == 0) {
            try {
                InputStream is1 = ctx.getAssets().open("cellbg.png");
                Drawable d1 = Drawable.createFromStream(is1, "cellbg");
                holder.l1.setBackgroundDrawable(d1);

            } catch (Exception e) {
                throw new Error(" exception in TableListAdapter "
                        + e.getMessage());
            }
        } else {
            try {
                InputStream is2 = ctx.getAssets().open("cellbg1.png");
                Drawable d2 = Drawable.createFromStream(is2, "cellbg1");
                holder.l1.setBackgroundDrawable(d2);
            } catch (Exception e) {
                throw new Error(" exception in TableListAdapter "
                        + e.getMessage());
            }
        }
        count++;
//setting the arrow
        if (i < updown.length) {
            if (updown[i] > 0) {
                holder.ivarrow.setImageResource(R.drawable.up);
                Log.v("up", "up");
            } else {
                holder.ivarrow.setImageResource(R.drawable.down);
                Log.v("down", "down");
            }
            Log.v("i=", i + " ,updown=" + updown[i]);
            i++;
        }

        holder.txtFirst.setText(map.get(FIRST_COLUMN));
        holder.txtSecond.setText(map.get(SECOND_COLUMN));
        holder.txtThird.setText(map.get(THIRD_COLUMN));
        return convertView;
    }

日志显示正确的数据,但箭头仅显示在第一行。即,如果列表的最后一个值为positive,则会向第一行显示up arrow,如果列表的最后一个值为negative,则会向第一行显示down arrow ... < / p>

如何向每行显示相应的箭头?

我是Android开发的新手。所以任何帮助都将是生命的救星!!!

screen

1 个答案:

答案 0 :(得分:4)

if (i < updown.length) {
        if (updown[i] > 0) 
            holder.ivarrow.setImageResource(R.drawable.up);
        else 
            holder.ivarrow.setImageResource(R.drawable.down);
        Log.v("i=", i + " ,updown=" + updown[i]);
        i++;
    }

用以下代码替换您的代码:

if (updown[position] > 0) 
            holder.ivarrow.setImageResource(R.drawable.up);
        else 
            holder.ivarrow.setImageResource(R.drawable.down);

我觉得它适合你