Search code examples
androidandroid-studioandroid-adapterbaseadapterandroid-gridview

android gridview doesn't show the data from retrofit


I am trying to get some data from an API and i want to show the category name and image in the gridview, the code can bring the data from the server but i think i have some problem at the adapter that inflates the gridview itself which is why it doesn't show anything at all.

Here's the adapter class for the code :

public class MyAdapter extends BaseAdapter{
List<DisplayData> data= Collections.emptyList();
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(Context context,List<DisplayData> displayDataList){
    layoutInflater=LayoutInflater.from(context);
    this.data=displayDataList;
    this.context=context;
}
@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int i) {
    return null;
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    DisplayData temp=data.get(i);
    View row=view;
    MyViewHolder holder=null;
    if(row==null){
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.grid_item,viewGroup,false);
        row.setTag(holder);
    }else{
        holder=(MyViewHolder)row.getTag();
    }
    byte[] qrimage = Base64.decode(temp.getImage().getBytes(),0);
    Bitmap bmp = BitmapFactory.decodeByteArray(qrimage, 0, qrimage.length);
    holder.imageView.setImageBitmap(bmp);
    holder.name.setText(temp.getCatName());
    return null;
}
 class MyViewHolder{
     ImageView imageView;
     TextView name;
     MyViewHolder(View view){
         imageView=(ImageView) view.findViewById(R.id.categoryImageIV);
         name=(TextView) view.findViewById(R.id.categoryNameTV);
     }
    }
  }

And this is where i get the response from the server using retrofit, the response comes and the textview shows the restaurant name which makes me sure the problem is not from here

public void getData(){
    //declare the retrofit with the base url and connect it to the interface and pojo class
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    DataAPI service= retrofit.create(DataAPI.class);
    Call<SyncData> call=service.requestData(id);
    //send the post request and check both cases if successful or failed
    call.enqueue(new Callback<SyncData>() {
        @Override
        public void onResponse(Response<SyncData> response, Retrofit retrofit) {
            try{
                SyncData syncData=response.body();
                restaurantName.setText(syncData.getRestaurantName());
                List<Category> categories= syncData.getCategories();
                System.out.println(categories.get(0).getCategoryName());

                for(int i=0;i<categories.size();i++){
                    //get the data of each row in the array
                    image=String.valueOf(categories.get(i).getCategoryImage());
                    categoryName=categories.get(i).getCategoryName();

                    //add the data of the row to the list of variables to be shown
                    DisplayData current=new DisplayData();
                    current.setCatName(categoryName);
                    current.setImage(image);
                    myDataSet.add(current);
                }
                adapter=new MyAdapter(Panel.this,myDataSet);
                gridView.setAdapter(adapter);
            }catch (Exception e){

            }
        }
        @Override
        public void onFailure(Throwable t) {

        }
    });
}

UPDATE: i have changed the getCount from return 0 to return data.size() as people have told me and now i get this error in the Base64 decode line in the adapter

FATAL EXCEPTION: main
              Process: com.example.omara.beintask, PID: 1571
              java.lang.IllegalArgumentException: bad base-64
                  at android.util.Base64.decode(Base64.java:161)
                  at android.util.Base64.decode(Base64.java:136)
                  at 
com.example.omara.beintask.MyAdapter.getView(MyAdapter.java:59)

Solution

  • You need to change in your adapter this method as adapter get counts for data from this method

    Insted of this code,

    @Override
    public int getCount() {
        return 0;
    }
    

    put this below one in your adapter class

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

    This will work and show your data into the GridView.

    To Show images using Glide, use the code given below:

    Glide.with(context)
                .load(SERVER_HOST + data.get(position).getImage())
                .into(holder?.imageView);