在getView方法中返回错误的位置

时间:2014-03-24 14:51:37

标签: android

我正在使用CustomGridAdapter扩展BAseAdapter以在列表视图中显示数组的值。但是在CustomGridAdapter的getView中,数组值的位置不在流中。

public class CustomGridAdapter extends BaseAdapter {

    private Context context; 
    private final String[] gridValues;
    private final String[] gridImage;

    //Constructor to initialize values
    public CustomGridAdapter(Context context, String[] gridValues, String[] gridImage) {
        this.context = context;
        this.gridValues = gridValues;
        this.gridImage=gridImage;
    }

    @Override
    public int getCount() {

        // Number of times getView method call depends upon gridValues.length
        return gridValues.length;
    }

    @Override
    public Object getItem(int position) {

        return gridValues[position];
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }


    // Number of times getView method call depends upon gridValues.length

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

        //LayoutInflator to call external grid_item.xml file

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from grid_item.xml
            gridView = inflater.inflate(R.layout.grid_item, null);

            // set value into textview

            TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
            textView.setText(gridValues[position]);
            Log.i(new Integer(position).toString(),gridValues[position] );
            ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
            Bitmap bitmap = BitmapFactory.decodeFile(path, options);
            grp_icon.setImageBitmap(bitmap);
            // set image based on selected text

            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            String mobile = gridValues[position];



        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }
}

在customGridAdapter的getView方法中,位置的顺序是0,1,2,0,4为什么我在2之后而不是3之后得到0?

3 个答案:

答案 0 :(得分:3)

@Override
public long getItemId(int position) {

    return position; //return position here
}

将你的getView设为,

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

    //LayoutInflator to call external grid_item.xml file

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from grid_item.xml
        gridView = inflater.inflate(R.layout.grid_item, null);

        // set value into textview

        TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
        textView.setText(gridValues[position]);
        Log.i(new Integer(position).toString(),gridValues[position] );
        ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);        



    } else {
        gridView = (View) convertView;
    }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        grp_icon.setImageBitmap(bitmap);
        // set image based on selected text

        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        String mobile = gridValues[position];

    return gridView;
}

您的converView仅对屏幕上最初显示的视图数量为空。滚动它们converView != null后,它们就会被回收。所以将代码移到它之外。

答案 1 :(得分:0)

你只是使用这样的,你会把它作为一个例子来编辑你的自定义适配器,你可以得到Out put我认为你在转换视图中做错了

你只是删除了对象的条件...... 并像这样使用

 public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
    super(context, resource);
    this.mlist = mlist;
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.listitem, null);
    TextView text1 = (TextView) convertView.findViewById(R.id.item1);
    TextView text2 = (TextView) convertView.findViewById(R.id.item2);
    text1.setText(mlist.get(position).getListitem1());
    text2.setText(mlist.get(position).getListitem2());
    text2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // you just put your Logic here And use this custom adapter to
            // load your Data By using this particular custom adapter to
            // your listview

        }
    });
    return convertView;
}

   }

答案 2 :(得分:0)

这与ListView和从ListView继承的类如何基于滚动位置和GC /性能原因的其他因素显示结果有关。文件中规定不保证订单或位置。为了解决这个问题,建议您使用ID而不是项目的位置进行引用。

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

有关更多信息,请参阅: https://www.youtube.com/watch?v=wDBM6wVEO70