java - Android Recycler 查看自定义适配器未执行

标签 java android xml android-studio android-recyclerview

我一直在尝试让自定义Recycler View 适配器工作。我不太明白为什么它不起作用。我觉得奇怪的是自定义适配器的代码不执行(因为当我在那里放置断点时它不会中断)并且我确定我绑定(bind)了自定义适配器回收器 View 。我希望有人能明白为什么它不起作用。

Activity :

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

}

行 View :

<?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>

带有回收器 View 的布局:

<?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>

感谢任何帮助。

最佳答案

两件事

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

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

关于java - Android Recycler 查看自定义适配器未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59831750/

相关文章:

java - Spring Integration http 出站网关 POST 响应不包含转义的符号

Python 列表到 XML,反之亦然

java - Tomcat 日志属性过滤

Java Thread.sleep() 无法正常工作

android - 更改 tabHost 上的文本大小

android - 通过 thread_id 取得联系

java - 已编译.Jar Crashes(无Java控制台),但在Eclipse中编译并运行?

java - 如何获取 BOX android api 中的可用空间?

Java Spring - 仅保存(POST)来自 ManyToOne 关系的 id

javascript - 未捕获的类型错误 : Cannot call method 'hasChildNodes' of undefined