我正在尝试从API获取一些数据,我想在gridview
中显示类别名称和图像,代码可以从服务器带来数据,但我认为我在适配器上有一些问题这会使gridview
本身膨胀,这就是为什么它根本不显示任何内容。
以下是代码的适配器类:
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);
}
}
}
这是我从服务器使用改造获得响应的地方,响应来了,textview
显示了餐馆名称,这使我确定问题不在这里
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) {
}
});
}
更新:我已经将getCount从返回0更改为返回data.size(),正如人们告诉我的那样,现在我在适配器的Base64解码行中出现此错误
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)
答案 0 :(得分:3)
问题出在这里,你总是返回0,调用这个方法来区分需要绘制的元素数量:
@Override
public int getCount() {
return 0;
}
应该是:
@Override
public int getCount() {
return data.size();
}
答案 1 :(得分:0)
您需要在适配器中更改此方法,因为适配器从此方法获取数据
这段代码的内容,
StaleElementReferenceException
将其放在适配器类
中@Override
public int getCount() {
return 0;
}
这样可以将您的数据显示在@Override
public int getCount() {
return data.size();
}
。
要使用GridView
显示图片,请使用以下代码:
Glide