我有一个自定义的ArrayList,其中包含大约80到90个数据。我正在使用RecyclerView来复制活动中的视图。但是,该视图中的文本视图会随着向下或向上滚动而更改。此外,我的数组列表中的所有数据甚至不会显示在适配器中。但是,我已经对列表进行了排序,并且列表按升序包含所有数据。Somtime 相同的数据一遍又一遍地重复。但是,如果数组列表中的数据很少,则它可以正确显示所有项目。我在这里错过了什么吗?
这是适配器和主活动以及我的 xml 和我的自定义数组列表的代码。
适配器:
public class Adapter_Usage extends RecyclerView.Adapter<Adapter_Usage.View>{
Context context;
CustomTextView txtsessiontime,txtvolupload,txtvoldownload,txtusageyear,txtusageday,txtusagemon;
ArrayList<Info_Usage> listusage;
public Adapter_Usage(Context context, int resource, ArrayList<Info_Usage> listusage) {
this.context = context;
this.listusage=listusage;
}
@Override
public View onCreateViewHolder(ViewGroup parent, int viewType) {
android.view.View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_usage, parent, false);
return new View(view);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(View holder, int position)
{
Info_Usage infousage = listusage.get(holder.getAdapterPosition());
String[] arraybilldate = infousage.strdate.split("\s+");
txtusageyear.setText(arraybilldate[2]);
txtusageday.setText(arraybilldate[1]);
txtusagemon.setText(arraybilldate[0]+", ");
txtsessiontime.setText(infousage.strsessiontime);
txtvolupload.setText("Upload : "+infousage.struploads+" MB");
txtvoldownload.setText("Download : "+infousage.strdownloads+" MB");
}
@Override
public int getItemCount() {
return listusage.size();
}
public class View extends RecyclerView.ViewHolder {
public View(android.view.View view) {
super(view);
txtusageyear=view.findViewById(R.id.txtusageyear);
txtusageday=view.findViewById(R.id.txtusageday);
txtusagemon=view.findViewById(R.id.txtusagemon);
txtsessiontime=view.findViewById(R.id.txtsessiontime);
txtvolupload=view.findViewById(R.id.txtvolupload);
txtvoldownload=view.findViewById(R.id.txtvoldownload);
}
}
}
自定义数组列表:
public class Info_Usage {
public String strdate,strdownloads,struploads,strsessiontime,strcurr;
public String getDate() {
return strdate;
}
public String getcurr() {
return strcurr;
}
}
我的活动:
Collections.sort(listusage, new Comparator<Info_Usage>()
{
@Override
public int compare(Info_Usage lhs, Info_Usage rhs)
{
int right=Integer.parseInt(rhs.getcurr());
int left=Integer.parseInt(lhs.getcurr());
return right<left?1:right>left?-1:0;
}
});
if(listusage.size()==0)
{
recycler.setVisibility(View.GONE);
txtnousage.setVisibility(View.VISIBLE);
}
else
{
txtnousage.setVisibility(View.GONE);
recycler.setVisibility(View.VISIBLE);
recycler.setAdapter(new Adapter_Usage(Activity_Usage.this, 0, listusage));
}
您的Adapter
中有几个问题。
-
在
onCreateViewHolder
内正确返回列表行视图:@Override public View onCreateViewHolder(ViewGroup parent, int viewType) { return LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_usage, parent, false); }
-
在
onBindViewHolder
内正确访问您的列表数据:@Override public void onBindViewHolder(View holder, int position) { Info_Usage infousage = listusage.get(position); String[] arraybilldate = infousage.strdate.split("\s+"); txtusageyear.setText(arraybilldate[2]); txtusageday.setText(arraybilldate[1]); txtusagemon.setText(arraybilldate[0]+", "); txtsessiontime.setText(infousage.strsessiontime); txtvolupload.setText("Upload : "+infousage.struploads+" MB"); txtvoldownload.setText("Download : "+infousage.strdownloads+" MB"); }