Baseapdapter的getview总是在android中返回0索引

时间:2013-12-24 12:58:11

标签: android adapter baseadapter

我有一个问题,即我的Adapter的getView方法总是返回0作为索引。因此,在我的GridView中只有一个图像显示2次。所以,请提出任何相同的解决方案。

代码:适配器

public class LibraryAdapter extends BaseAdapter{

    Context _context;
    ArrayList<String> listMags;
    HashMap<String, ArrayList<String>> hashMap;

    public LibraryAdapter(Context context, ArrayList<String> listMags, HashMap<String, ArrayList<String>> hashMap)
    {
        _context = context;
        this.listMags = listMags;
        this.hashMap = hashMap;
    }

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

    @Override
    public Object getItem(int arg0)
    {
        return listMags.get(arg0);
    }

    @Override
    public long getItemId(int arg0)
    {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2)
    {
        ItemHolder itemHolder;
        String magName = listMags.get(arg0);
        if (arg1 == null)
        {
            LayoutInflater layoutInflaterObj = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemHolder = new ItemHolder();
            arg1 = layoutInflaterObj.inflate(R.layout.library_grid_item, null);

            itemHolder.imgMag = (ImageView) arg1.findViewById(R.id.imgIssuesGridItem);

            itemHolder.tvMagName = (TextView) arg1.findViewById(R.id.tvIssuesDate);
            arg1.setTag(itemHolder);
        }
        else
        {
            itemHolder = (ItemHolder) arg1.getTag();
        }
        Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+"/"+hashMap.get(magName).get(0)+"/"+(hashMap.get(magName).get(0).replace(magName, ""))+arg0+".png");
        //itemHolder.imgIssueItem.setImageUrl(getItem(position).getImage_path(), imageLoader);
        itemHolder.imgMag.setImageBitmap(bmp);
        itemHolder.tvMagName.setText((new File(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+"/"+hashMap.get(magName).get(0))).getName());

        return arg1;
    }

    static class ItemHolder
    {
        ImageView imgMag;
        TextView tvMagName;
    }
}

提前致谢。

3 个答案:

答案 0 :(得分:0)

尝试覆盖getItemId()方法,如下所示:

@Override
public long getItemId(int arg0)
{
    return arg0;
}

答案 1 :(得分:0)

更改以下行除了显示位置0之外添加arg0,如下所示:

 Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+"/"+hashMap.get(magName).get(arg0)+"/"+(hashMap.get(magName).get(arg0).replace(magName, ""))+arg0+".png");

 itemHolder.imgMag.setImageBitmap(bmp);
 itemHolder.tvMagName.setText((new File(Environment.getExternalStorageDirectory()+"/MAGZ/"+magName+"/"+hashMap.get(magName).get(arg0))).getName());

并更改您的getItemId方法,如下所示:

@Override
public long getItemId(int arg0)
{
    return arg0;
}

答案 2 :(得分:0)

你已经给出了返回0

试试这个

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