在GridView中获取项目时出错

时间:2016-01-22 05:32:52

标签: android android-studio

Got this Expected this

我有4个ArrayList数据在GridView中显示它.ArrayList数据是

  ArrayList<Integer> imgShopsImg = new ArrayList<Integer>();
                    imgShopsImg.add(R.color.red);    //0
                    imgShopsImg.add(R.color.green);  //1
                    imgShopsImg.add(R.color.blue);   //2
                    imgShopsImg.add(R.color.pink);   //3

但是当我尝试使用适配器类在Gridview中显示时,我只获取最后一个值(PINK)。我正在努力解决这个问题。

SalesAdapter Class。

public class SalesAdapter extends BaseAdapter {

    ArrayList<Integer> imgShopsImg;
    Context context;
    ArrayList<String> shopname;
    ArrayList<String> shoparea;
    ArrayList<String> lastsync;
    ArrayList<String> amount;
    ArrayList<String> urll;
    ArrayList<String> dp;
    ArrayList<String> dpunm;
    ArrayList<String> dppwd;
    String x = "\u20B9" + " ";

    public SalesAdapter(Context context, ArrayList<String> shopName, ArrayList<String> shopArea,
                        ArrayList<String> lasts, ArrayList<String> totamount,
                        ArrayList<String> urr, ArrayList<String> dppp,
                        ArrayList<String> dpuuu, ArrayList<String> dpppwd, ArrayList<Integer> imgShopsImg) {

        this.context = context;
        this.shopname = shopName;
        this.shoparea = shopArea;
        this.lastsync = lasts;
        this.amount = totamount;
        this.urll = urr;
        this.dp = dppp;
        this.dpunm = dpuuu;
        this.dppwd = dpppwd;
        this.imgShopsImg = imgShopsImg;
    }

    @Override
    public int getCount() {
        return shoparea.size();
    }

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

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

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.mainlistitem, null);
        }
        final TextView itemName = (TextView) convertView.findViewById(R.id.shopname);
        final TextView itemId = (TextView) convertView.findViewById(R.id.amount);
        final TextView itemMinPrice = (TextView) convertView.findViewById(R.id.sync);
        final TextView itemLocation = (TextView) convertView.findViewById(R.id.txtShopLocation);
        final TextView posurl = (TextView) convertView.findViewById(R.id.url);
        final TextView posdb = (TextView) convertView.findViewById(R.id.dp);
        final TextView posdpunm = (TextView) convertView.findViewById(R.id.dpunm);
        final TextView posdppwd = (TextView) convertView.findViewById(R.id.dppwd);
        final ImageView imageShop = (ImageView) convertView.findViewById(R.id.img_shops);

        Log.e("Adapter Checking...", String.valueOf(shopname) + " " + shoparea + " " + lastsync + " " + amount + " " + imgShopsImg + " " + position);

        itemName.setText(shoparea.get(position));
        itemLocation.setText(shopname.get(position));
        itemId.setText(x + String.valueOf(amount.get(position)));

        Log.e("LastSync", String.valueOf(lastsync + "Position" + position));
        if (lastsync.get(position).contains("Just Now"))
            itemMinPrice.setText("Last Updated: " + String.valueOf(lastsync.get(position)));
        else
            itemMinPrice.setText("Last Updated: " + String.valueOf(lastsync.get(position)) + "ago");
        posurl.setText(String.valueOf(urll.get(position)));
        posdb.setText(String.valueOf(dp.get(position)));
        posdpunm.setText(String.valueOf(dpunm.get(position)));
        posdppwd.setText(String.valueOf(dppwd.get(position)));
        imageShop.setImageResource(imgShopsImg.get(position));
        return convertView;
    }
}

2 个答案:

答案 0 :(得分:3)

删除以下代码

final ImageView imageShop = (ImageView) convertView.findViewById(R.id.img_shops);

imageShop.setImageResource(imgShopsImg.get(position));

并将其移至if (convertView == null)

之外

答案 1 :(得分:0)

我认为

convertView = mInflater.inflate(R.layout.mainlistitem, null);

应该是

convertView = mInflater.inflate(R.layout.mainlistitem, false);

因为它的布尔表达式