如何在androidstudio中从api中检索列表值

klh5stk1  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(359)

我正在尝试使用wikimediarestapi来检索androidstudio中的“特色图片”。json文件返回 List<Imageinfo> ,其中包含不同类型的数据。下面是我用来获取数据的适配器:

public class ItemTypeAdapter extends RecyclerView.Adapter<ItemTypeAdapter.ItemtypeViewHolder> {
    private Context context;
    private List<Imageinfo> dataretrivals;

    public ItemTypeAdapter(Context context, List<Imageinfo> dataretrivals) {
        this.context = context;
        this.dataretrivals = dataretrivals;
    }

    @NonNull
    @Override
    public ItemtypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_type1,parent,false);
        return new ItemtypeViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ItemtypeViewHolder holder, int position) {
        Imageinfo data = dataretrivals.get(position);
        try{
            holder.listView.addView((android.view.View) data.getQuery().getPages().get4406048().getImageinfo());

        }
        catch (NullPointerException ignored)
        {

        }

    }

    public int getItemCount() {
        return dataretrivals.size();
    }

    public class ItemtypeViewHolder extends RecyclerView.ViewHolder
    {
        TextView title,description;
        ImageView image;
        ListView listView;
        public ItemtypeViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.imagetitle);
            description = itemView.findViewById(R.id.imagediscription);
            image = itemView.findViewById(R.id.imagetype1);
            listView = itemView.findViewById(R.id.Listview);

        }
    }
}

这将产生错误

java.lang.ClassCastException:java.util.ArrayList cannot be cast to android.view.View
        at com.example.heterogeneousrvdemo.Adapter.ItemTypeAdapter.onBindViewHolder(ItemTypeAdapter.java:42)
        at com.example.heterogeneousrvdemo.Adapter.ItemTypeAdapter.onBindViewHolder(ItemTypeAdapter.java:20)

请告诉我如何检索和显示 List<imageinfo> java中的数据类型。

q9rjltbz

q9rjltbz1#

我相信这会解决你的问题。

public AirlinesListFragment() {
    airlines = new ArrayList<>();

  }

请查看此页以了解解决方案:无法在fragment类中获取操作栏

相关问题