Android Пользовательский адаптер Recycler не выполняется - PullRequest
0 голосов
/ 21 января 2020

Я пытался заставить работать пользовательский 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 ammount;
    String name, table;

    pay_item(int time, double ammount, String name, String table) {
        this.time = time;
        this.ammount = ammount;
        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).ammount));
        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>

Любая помощь приветствуется.

1 Ответ

1 голос
/ 21 января 2020

Две вещи

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

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