用Recycleview替换Listview后,我的列表不会显示

时间:2015-10-21 08:51:25

标签: java android listview adapter android-recyclerview

所以我试图用recyclerview替换我的listview以改进我的应用, 现在我的活动没有显示列表。 我正在检查适配器,我找不到任何错误

这是我的 ListView适配器:

公共类CustomAdapter扩展了BaseAdapter {

private Context context;
private List<Salary> salaryList;

public CustomAdapter(Context context, List<Salary> salaryList) {
    this.salaryList = salaryList;
    this.context = context;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item_card, parent, false);
        holder = new ViewHolder();
        holder.fieldName = (TextView) view.findViewById(R.id.fieldName);
        holder.txtSalary = (TextView) view.findViewById(R.id.txtSalary);
        holder.cardview = (CardView) view.findViewById(R.id.cardview);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    Salary salary = salaryList.get(position);
    holder.salary = salary;
    holder.cardview.setTag(salary);
    holder.fieldName.setText(salary.getField());
    holder.txtSalary.setText(salary.getSalary());

    holder.cardview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Salary salary = (Salary) v.getTag();
            Intent i = new Intent(context, ViewSalary.class);
            i.putExtra(ViewSalary.FIELD_NAME, salary.getField());
            i.putExtra(ViewSalary.SALARY_NUM, salary.getSalary())
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    });

public static class ViewHolder {
    Salary salary;
    TextView fieldName;
    TextView txtSalary;
    CardView cardview;
}

}

现在这是我的新 RecyclerView适配器

公共类CustomAdapter扩展了RecyclerView.Adapter {

private List<Salary> salaryList;

public CustomAdapter(List<Salary> salaryList) {
    this.salaryList = new ArrayList<Salary>();
    this.salaryList.addAll(salaryList);
}
@Override
public void onBindViewHolder(SalaryViewHolder salaryViewHolder, int i) {
    Salary salary = salaryList.get(i);
    salaryViewHolder.fieldName.setText(salary.getField());
    salaryViewHolder.txtSalary.setText(salary.getSalary());
    salaryViewHolder.salary = salary;
    salaryViewHolder.cardview.setTag(salary);

}


@Override
public SalaryViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.item_card, viewGroup, false);

    return new SalaryViewHolder(itemView);
}



public class SalaryViewHolder extends RecyclerView.ViewHolder{
    protected Salary salary;
    protected TextView fieldName;
    protected TextView txtSalary;
    protected CardView cardview;
    public SalaryViewHolder(View view) {
        super(view);
        fieldName = (TextView) view.findViewById(R.id.fieldName);
        txtSalary = (TextView) view.findViewById(R.id.txtSalary);
        cardview = (CardView) view;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Salary salary = (Salary) v.getTag();
                Intent i = new Intent(context, ViewSalary.class);
                i.putExtra(ViewSalary.FIELD_NAME, salary.getField());
                i.putExtra(ViewSalary.SALARY_NUM, salary.getSalary());;
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        });
    }
}

}

任何想法??

0 个答案:

没有答案