android–recyclerView.setAdapter不接受ArrayAdapter

android–recyclerView.setAdapter不接受ArrayAdapter,第1张

概述我试图用简单的String数据填充RecyclerView但是recyclelerView.setAdapter(适配器);给出错误.有什么问题?我是recyclerview的新手.RecyclerViewrecyclerView;String[]data={"ankush","kapoor"};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){supe

我试图用简单的String数据填充RecyclerVIEw但是recyclelerVIEw.setAdapter(适配器);给出错误.有什么问题?

我是recyclervIEw的新手.

RecyclerVIEw recyclerVIEw;String []data = {"ankush", "kapoor"};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.myRecyclerVIEw);    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, androID.R.layout.simple_List_item_1, data);    recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));    recyclerVIEw.setAdapter(adapter);}

解决方法:

您必须为RecyclerVIEw创建自定义Adapter类.以下是RecyclerVIEw自定义适配器类的示例:

public class CustomAdapter extends RecyclerVIEw.Adapter<CustomAdapter.MyVIEwHolder>{private LayoutInflater inflater;private Context context;<your string array List>public CustomAdapter(Context context,<your string arrayList>) {    inflater = LayoutInflater.from(context);    this.context = context;    this.<your string arrayList>=<your string arrayList>;}@OverrIDepublic MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {    VIEw vIEw = inflater.inflate(R.layout.custom_layout, parent, false);    MyVIEwHolder holder = new MyVIEwHolder(vIEw);    return holder;}@OverrIDepublic voID onBindVIEwHolder(MyVIEwHolder holder, int position) {         holder.serial_number.setText(<your string array[position]>);}@OverrIDepublic int getItemCount() {    return <size of your string array List>;}class MyVIEwHolder extends RecyclerVIEw.VIEwHolder{    TextVIEw serial_number;    public MyVIEwHolder(VIEw itemVIEw) {        super(itemVIEw);        serial_number = (TextVIEw)itemVIEw.findVIEwByID(R.ID.serialNo_CL);        }    }}
总结

以上是内存溢出为你收集整理的android – recyclerView.setAdapter不接受ArrayAdapter全部内容,希望文章能够帮你解决android – recyclerView.setAdapter不接受ArrayAdapter所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存