0

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)
  • 1
    THe answer for your update is here https://stackoverflow.com/questions/25217515/java-lang-illegalargumentexception-bad-base-64. The base 64 string you're trying to decode is not valid – Levi Moreira Mar 09 '18 at 13:25

2 Answers2

3

The problem is in here, you're always returning 0, this method is called to indetify the number of elements that need to be drawn:

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

It should be:

@Override
public int getCount() {
    return data.size();
}
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • i am not even calling it though –  Mar 09 '18 at 13:14
  • You don't call it, the adapter calls it internally :). You need only to return the amount of data you want to display – Levi Moreira Mar 09 '18 at 13:15
  • i have made the change and now i am getting an error in the base64 decode –  Mar 09 '18 at 13:19
  • That's because now your getView() code is being called and there's an error in it. It wasn't called before because you said to the adapter that it shouldn't render any view (returned 0 from the count function). You'll need to revaluate the code – Levi Moreira Mar 09 '18 at 13:22
0

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);
Chintak Patel
  • 748
  • 6
  • 24
  • i have made the change and now i am getting an error in the base64 decode –  Mar 09 '18 at 13:19
  • It is happening at `byte[] qrimage = Base64.decode(temp.getImage().getBytes(),0);` line. Please check if your image data is proper or not ? – Chintak Patel Mar 09 '18 at 13:25
  • this is the json for the image i am trying to get "categoryImage": "images/Tue-Jul-2017-596dd1a1d1bf8.jpg" –  Mar 09 '18 at 13:27
  • So you can use `Glide` library for showing images on `ImageView`. – Chintak Patel Mar 09 '18 at 13:29
  • i am checking out how to use glide first –  Mar 09 '18 at 13:34
  • add `implementation 'com.github.bumptech.glide:glide:3.8.0'` to your `build.gradle(app)` file dependecy and after sync, Use the edited code in post to show image. – Chintak Patel Mar 09 '18 at 13:36
  • what is that variable "SERVER_HOST" –  Mar 09 '18 at 13:42
  • `SERVER_HOST` means if you are having image on specific server like `http://example.org/` and your `data.get(position).getImage()` is the last image path which is in your case is `images/Tue-Jul-2017-596dd1a1d1bf8.jpg` – Chintak Patel Mar 09 '18 at 13:45
  • thank u, got it now, although the api is kind of not loading the images –  Mar 09 '18 at 13:49
  • this is the api documentation, mind just trying out an image link and telling me if it's actually loading or not? http://www.beinmedia.com/okmenu/trial_doc.html#users-synchronize-data-post –  Mar 09 '18 at 13:51