android gridview 不显示改造后的数据
android gridview doesn't show the data from retrofit
我正在尝试从 API 获取一些数据,我想在 gridview
中显示类别名称和图像,代码可以从服务器获取数据,但我想我适配器有一些问题使 gridview
本身膨胀,这就是为什么它根本不显示任何内容的原因。
这是代码的适配器 class:
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() 现在我在适配器
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,调用这个方法来确定需要绘制的元素个数:
@Override
public int getCount() {
return 0;
}
应该是:
@Override
public int getCount() {
return data.size();
}
您需要在适配器中更改此方法,因为适配器从该方法获取数据计数
已安装此代码,
@Override
public int getCount() {
return 0;
}
把这个放在你的适配器下面class
@Override
public int getCount() {
return data.size();
}
这将起作用并将您的数据显示到 GridView
。
要使用 Glide
显示图像,请使用下面给出的代码:
Glide.with(context)
.load(SERVER_HOST + data.get(position).getImage())
.into(holder?.imageView);
我正在尝试从 API 获取一些数据,我想在 gridview
中显示类别名称和图像,代码可以从服务器获取数据,但我想我适配器有一些问题使 gridview
本身膨胀,这就是为什么它根本不显示任何内容的原因。
这是代码的适配器 class:
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() 现在我在适配器
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,调用这个方法来确定需要绘制的元素个数:
@Override
public int getCount() {
return 0;
}
应该是:
@Override
public int getCount() {
return data.size();
}
您需要在适配器中更改此方法,因为适配器从该方法获取数据计数
已安装此代码,
@Override
public int getCount() {
return 0;
}
把这个放在你的适配器下面class
@Override
public int getCount() {
return data.size();
}
这将起作用并将您的数据显示到 GridView
。
要使用 Glide
显示图像,请使用下面给出的代码:
Glide.with(context)
.load(SERVER_HOST + data.get(position).getImage())
.into(holder?.imageView);