我无法在我的Recycler视图中获取数据。我对android很新,并尝试在tablayout中实现此循环器视图但没有获取任何数据。有三个选项卡,并尝试使用一个选项卡的此代码。请帮助我与此同在。谢谢。
我的代码如下。
我为recyler视图创建了两个xml文件,另一个用于自定义行。 下面是我的活动代码。
我的片段活动
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private vivzadapter adapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView= (RecyclerView)layout.findViewById(R.id.lis);
adapter = new vivzadapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data =new ArrayList<>();
int icons=(R.drawable.icon);
String[] titles = {"Ashish", "shivam", "yogesh", "ravi"};
for (int i=0;i<titles.length;i++) {
Information information = new Information();
information.iconId=icons;
information.title=titles[i];
data.add(information);
}
return data;
}
}
我的适配器活动 -
public class vivzadapter extends RecyclerView.Adapter<vivzadapter.MyViewHolder>{
private final LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public vivzadapter(Context context,List<Information> data){
inflater =LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= inflater.inflate(R.layout.customrow, parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder ;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconId);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title =(TextView) itemView.findViewById(R.id.textvw);
icon =(ImageView) itemView.findViewById(R.id.imageView);
}
}
}
答案 0 :(得分:2)
更改
@Override
public int getItemCount() {
return 0;
}
为:
@Override
public int getItemCount() {
return data.size();
}