我试图用简单的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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)