使用适配器的RecyclerView项目复制

时间:2018-01-22 10:07:05

标签: android arraylist android-recyclerview android-adapter

我有一个包含大约80到90个数据的自定义ArrayList。我正在使用RecyclerView复制Activity中的视图。但是,该视图中的textview会更改为向下或向上滚动。此外,我的arraylist中的所有数据都不会出现在适配器中。但是,我已经对列表进行了排序,列表中按升序包含了所有数据。同时,相同的数据反复重复。但如果arraylist中的数据很少,那么它会正确地显示所有项目。我想念这里的东西。

以下是适配器和主要活动以及我的xml和我的自定义arraylist的代码。

适配器:

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);
    }
}
}

自定义Arraylist:

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));
            }

1 个答案:

答案 0 :(得分:0)

您的Adapter内有几个问题。

  1. onCreateViewHolder内正确返回列表行视图:

    @Override
    public View onCreateViewHolder(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_usage, parent, false);
    }
    
  2. 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");
    }