getView从未调用过Baseadapter

时间:2014-11-25 10:07:26

标签: android gridview baseadapter

搜索了最常见的类似主题,但没有任何帮助。

public class NewCustomAdapter extends BaseAdapter {

    Context activityContext;
    LayoutInflater myInflater;
    Drawable[] myDrawableArray;
    String[] myLabelArray;
    String[] myABCArray;
    int position = 0;
    ImageView myImg;
    TextView myLabelTextView;
    TextView myABCTextView;

    public NewCustomAdapter(Context context) {

        // TODO Auto-generated constructor stub
        this.activityContext = context;
        myInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        // TODO Auto-generated method stub
        Log.e("getcount","true  " + position);
        return position;
    }

    @Override
    public Object getItem(int position) {

        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {

        // TODO Auto-generated method stub
        return position;
    }

    public class Holder {

        ImageView img;
        TextView tv;
        TextView abc;

        public Holder(ImageView img, TextView tv, TextView abc) {

            this.img = img;
            this.tv = tv;
            this.abc = abc;
        }
    }

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

        // TODO Auto-generated method stub
        Log.e("getView","true");
        if(convertView == null) {

            convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, null);
            myImg = (ImageView)     convertView.findViewById(R.id.imageview_layout_normal_portrait_sizemedium);
            myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizemedium);
            myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizemedium);
            convertView.setTag(new Holder(myImg,myLabelTextView, myABCTextView));
        } else {

            Holder holder = (Holder) convertView.getTag();
            myImg = holder.img;
            myLabelTextView = holder.tv;
            myABCTextView = holder.abc;
        }
        myImg.setImageDrawable(myDrawableArray[position]);
        myLabelTextView.setText(myLabelArray[position]);
        myABCTextView.setText(myABCArray[position]);

        return convertView;
    }

    public void updateGridView(Drawable[] myDrawableArray_, String[] myLabelArray_, String[] myABCArray_, int position_) {

        this.myDrawableArray = myDrawableArray_;
        this.myLabelArray = myLabelArray_;
        this.myABCArray = myABCArray_;
        this.position = position_;
        this.notifyDataSetChanged();
    }
}

它是一个普通的BaseAdapter,它从我的backgroundThread获取更新。我的BackgroundThread调用

updateGridView(...)

方法。方法按需要调用。每次调用updateGridView时都会调用getCount ..这样也可以。唯一的问题是。 GridView不会膨胀。永远不会调用getView。

在我的AsyncTask中我正确设置了UIThread(onPreExecute()):

gv = (GridView) myActivityView.findViewById(R.id.gv1);
myCustomAdapter = new NewCustomAdapter(myActivityContext);
gv.setAdapter(myCustomAdapter);

没有错误,getView永远不会被调用,这就是为什么gridview永远不会被夸大......感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

getCount()返回列表数据的大小,并正确实现ListView性能的ViewHolder设计模式:

@Override
public int getCount() {
    return myDrawableArray.length;
}

示例:

public class NewCustomAdapter extends BaseAdapter {

    Context context;
    Drawable[] myDrawableArray;
    String[] myLabelArray;
    String[] myABCArray;
    int position;
    public NewCustomAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return myDrawableArray.length;
    }

    @Override
    public Object getItem(int position) {
        return myDrawableArray[position];
    }

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

    public class Holder {
        ImageView img;
        TextView tv;
        TextView abc;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.customadapter_view_layout_medium, null);
            holder.img = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizemedium);
            holder.tv = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizemedium);
            holder.abc = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizemedium);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.img.setImageDrawable(myDrawableArray[position]);
        holder.tv.setText(myLabelArray[position]);
        holder.abc.setText(myABCArray[position]);

        return convertView;
    }

    public void updateGridView(Drawable[] myDrawableArray_, String[] myLabelArray_, String[] myABCArray_, int position_) {
        this.myDrawableArray = myDrawableArray_;
        this.myLabelArray = myLabelArray_;
        this.myABCArray = myABCArray_;
        this.position = position_;

        this.notifyDataSetChanged();
    }
}

答案 1 :(得分:1)

如果getView返回getCount

,则会调用

!= 0

答案 2 :(得分:0)

您需要在getCount方法中返回总计数

由于您的第一个位置为0,因此getCount方法返回0,并且适配器认为列表中没有要显示的元素。