1

why the getView method called only once ? and return nothing ?

here is my code

public class GridViewAdapter extends BaseAdapter {

Context mContext;
ArrayList<String> list;

GridViewAdapter(Context context,ArrayList<String> list){
    this.mContext = context;
    this.list = new ArrayList<>(list);
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

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

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

    ImageView view = (ImageView) convertView;

    if(view==null){
        view = new ImageView(mContext);
    }
    Picasso.with(mContext).load(String.valueOf(list.get(position))).resize(360, 512).into(view);
    return view;
}

}

note : the size of arrayList passed is 20 ! so it's not empty list !

Mohamed Yehia
  • 400
  • 1
  • 5
  • 15

1 Answers1

0

Try extending

ArrayAdapter<String>
Terry W
  • 203
  • 3
  • 9
  • i trying to build a custom adapter , what is wrong with extending BaseAdapter class ! – Mohamed Yehia Mar 20 '16 at 20:58
  • ArrayAdapter extends BaseAdapter and implements most of the required functionality. – Terry W Mar 20 '16 at 21:05
  • Also, when you create your image view, you are using the basic one argument constructor, sometimes this is not enough for android. Might I suggest instead of creating a singular view, you create an xml layout with an ImageView in, then inflate that layout to get your return view – Terry W Mar 20 '16 at 21:07
  • and finally getItemId(position) should not return the position, as this is not an id associated with a view, by extending ArrayAdapter, you do not need to implement this method – Terry W Mar 20 '16 at 21:10
  • i tried it out ! here is my new code ! please check it https://gist.github.com/yehiahd/5234372e327453b76d51 – Mohamed Yehia Mar 20 '16 at 21:35
  • It sure looks ok.... are you 100% sure you are passing more than 1 item in the array, and are you 100% sure it's only being called once? – Terry W Mar 20 '16 at 21:39
  • when i make Toast message inside it with the size of passed Array ! it Toasts number 20 only once ! and when i Toast the position i get 0 only – Mohamed Yehia Mar 20 '16 at 21:51
  • instead of toast, use the logcat instead as multiple calls to Toast can be inconsistent. The View you are using the adapter for.... does show one view only too? – Terry W Mar 20 '16 at 21:56
  • D/Yehia:http://image.tmdb.org/t/p/w185//inVq3FRqcYIRl2la8iZikYYxFNR.jpg only i got this ! so , yes that getView called only once ! and return no View ! :( – Mohamed Yehia Mar 20 '16 at 22:16
  • hmmmmm strange.... I take it you're using a GridView? could you show me the xml for the GridView? – Terry W Mar 20 '16 at 22:23
  • first please what about this line in logcat ! Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value. i checked it out and founded that picasso is the problem ! – Mohamed Yehia Mar 20 '16 at 22:39
  • sorry, I thought Picasso may have been one of your Custom classes, (not heard of it before) is it making the app crash? I have zero idea about Picasso, personally I use NetworkImageView one of many examples can be found here http://www.truiton.com/2015/03/android-volley-imageloader-networkimageview-example/ – Terry W Mar 20 '16 at 22:52
  • really thank you so much for you answers and your patience i am going to check the link you provided me with once again , Thank you :D – Mohamed Yehia Mar 20 '16 at 22:58
  • No worries buddy, glad to help :) – Terry W Mar 20 '16 at 22:59
  • I faced this problem recently, and it was because I hadn't set `numColumns` in my `GridView` – Vedavyas Bhat Sep 25 '17 at 16:08