MMike
MMike

Reputation: 598

getView never called Baseadapter

searched the most common similar Topics but nothing helped.

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();
    }
}

It's a normal BaseAdapter which gets update from my backgroundThread. My BackgroundThread calls

updateGridView(...)

method. Method is called as wanted. getCount is also called everytime i call updateGridView.. so that works as well. Only Problem is. GridView doesn't inflate. getView is never called.

in my AsyncTask i set correctly on UIThread(onPreExecute()):

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

No Errors, getView is just never called, that's why gridview is never inflated... Any help is appreciated.

Upvotes: 0

Views: 1477

Answers (3)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

getCount() return size of list data and also properly implement ViewHolder design pattern for ListView performance :

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

Example :

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();
    }
}

Upvotes: 2

Sarthak Mittal
Sarthak Mittal

Reputation: 6114

You need to return the total count in getCount method

Since your first position is 0 your getCount method returns 0 and the adapter thinks that there are no elements to be displayed in the list.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157477

getView is called if getCount returns something != 0,

Upvotes: 1

Related Questions