【问题标题】:Android Recycler view custom adapter not executingAndroid Recycler视图自定义适配器未执行
【发布时间】:2020-01-20 22:32:11
【问题描述】:

我一直在尝试让自定义 Recycler view adapter 工作。我不太明白为什么它不起作用。我觉得奇怪的是custom adapter 的代码没有执行(因为当我在那里设置断点时它不会中断),我确定我将custom adapter 绑定到recycler view。我希望有人能明白为什么它不起作用。

活动:

public class payment_history extends AppCompatActivity {

    RecyclerView list;
    ArrayList<pay_item> list_data;
    pay_adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment_history);

        list_data = new ArrayList<>();

        list_data.add(new pay_item(10, 100, "jow", "10"));
        list_data.add(new pay_item(10, 100, "joe", "10"));
        list_data.add(new pay_item(10, 100, "joe", "10"));

        list = findViewById(R.id.payment_history_list);

        list.setLayoutManager(new LinearLayoutManager(this));

        adapter = new pay_adapter(list_data);
        list.setHasFixedSize(true);

        list.setAdapter(adapter);

    }
}

class pay_item {
    int time;
    double amount;
    String name, table;

    pay_item(int time, double amount, String name, String table) {
        this.time = time;
        this.amount = amount;
        this.name = name;
        this.table = table;
    }
}

适配器:

class pay_adapter extends RecyclerView.Adapter<pay_adapter.viewholder> {

    private ArrayList<pay_item> data;

    pay_adapter(ArrayList<pay_item> in) {
        data = in;
    }

    @NonNull
    @Override
    public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.payment_history_item, parent, false);
        return new viewholder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull viewholder holder, int position) {
        holder.name.setText(data.get(position).name);
        holder.table.setText(data.get(position).table);
        holder.amm.setText(String.valueOf(data.get(position).amount));
        holder.time.setText(data.get(position).time);
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    static class viewholder extends RecyclerView.ViewHolder {
        TextView time, amm, name, table;

        viewholder(View view) {
            super(view);
            time = view.findViewById(R.id.pay_time);
            amm = view.findViewById(R.id.pay_amount);
            name = view.findViewById(R.id.pay_name);
            table = view.findViewById(R.id.pay_table);
        }
    }

}

行视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="16dp"
    android:paddingEnd="16dp">

    <TextView
        android:id="@+id/pay_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Time"
        android:textColor="#000000"
        android:textSize="36sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/pay_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textColor="#000000"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/pay_table"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="6dp"
            android:text="table" />
    </LinearLayout>

    <TextView
        android:id="@+id/pay_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0"
        android:text="Amount"
        android:textColor="@color/colorPrimary"
        android:textSize="36sp" />

</LinearLayout>

带有回收站视图的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".payment_history">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/payment_history_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

感谢任何帮助。

【问题讨论】:

  • 您正在返回 getItemCount() 中的 0。将其更改为data.size()。

标签: java android xml android-studio android-recyclerview


【解决方案1】:

两件事

pay_adapter(ArrayList<pay_item> in) {
    data = in;
}

@Override
public int getItemCount() {
    return data.size() ;
}  

【讨论】:

  • 我认为构造函数中的notifyDataSetChanged() 不正确。请三思而后行,并相应地编辑您的答案。
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多