85 lines
2.8 KiB
Java
85 lines
2.8 KiB
Java
package com.example.bdkipoc;
|
|
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.TextView;
|
|
import android.widget.LinearLayout;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import java.util.List;
|
|
import java.text.NumberFormat;
|
|
import java.util.Locale;
|
|
|
|
public class TransactionAdapter extends RecyclerView.Adapter<TransactionAdapter.TransactionViewHolder> {
|
|
private List<TransactionActivity.Transaction> transactionList;
|
|
private OnPrintClickListener printClickListener;
|
|
|
|
public interface OnPrintClickListener {
|
|
void onPrintClick(TransactionActivity.Transaction transaction);
|
|
}
|
|
|
|
public TransactionAdapter(List<TransactionActivity.Transaction> transactionList) {
|
|
this.transactionList = transactionList;
|
|
}
|
|
|
|
public void setPrintClickListener(OnPrintClickListener listener) {
|
|
this.printClickListener = listener;
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public TransactionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_transaction, parent, false);
|
|
return new TransactionViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull TransactionViewHolder holder, int position) {
|
|
TransactionActivity.Transaction t = transactionList.get(position);
|
|
|
|
// Set reference ID
|
|
holder.referenceId.setText(t.referenceId);
|
|
|
|
// Format the amount as Indonesian Rupiah
|
|
try {
|
|
double amountValue = Double.parseDouble(t.amount);
|
|
NumberFormat rupiahFormat = NumberFormat.getInstance(new Locale("id", "ID"));
|
|
holder.amount.setText("Rp. " + rupiahFormat.format(amountValue));
|
|
} catch (NumberFormatException e) {
|
|
holder.amount.setText("Rp. " + t.amount);
|
|
}
|
|
|
|
holder.itemView.setOnClickListener(v -> {
|
|
if (printClickListener != null) {
|
|
printClickListener.onPrintClick(t);
|
|
}
|
|
});
|
|
|
|
// Set click listener for print button
|
|
holder.printSection.setOnClickListener(v -> {
|
|
if (printClickListener != null) {
|
|
printClickListener.onPrintClick(t);
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return transactionList.size();
|
|
}
|
|
|
|
static class TransactionViewHolder extends RecyclerView.ViewHolder {
|
|
TextView amount, referenceId;
|
|
LinearLayout printSection;
|
|
|
|
public TransactionViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
amount = itemView.findViewById(R.id.textAmount);
|
|
referenceId = itemView.findViewById(R.id.textReferenceId);
|
|
printSection = itemView.findViewById(R.id.printSection);
|
|
}
|
|
}
|
|
} |