【发布时间】:2018-03-09 13:08:02
【问题描述】:
我正在尝试从 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 从 return 0 更改为 return 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)
【问题讨论】:
-
您的更新答案在这里stackoverflow.com/questions/25217515/…。您尝试解码的 base 64 字符串无效
标签: android android-studio android-adapter baseadapter android-gridview