Saya sedang mengembangkan aplikasi Android untuk pemesanan online. Pengguna perlu mengklik item makanan di recyclerView dan saya mencoba mengirim ID makanan ke AddOrderActivity. Bagaimana saya bisa mendapatkan FId dan meneruskannya ke aktivitas baru?

CustomerHomeFragment.java

Public class CustomerHomeFragment extends Fragment {

private RecyclerView recyclerView;
private FoodsAdapter adapter;
private List<Food> foodList;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home_customer, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


    Call<FoodsResponse> call = RetrofitClient.getInstance().getApi().getFoods();

    call.enqueue(new Callback<FoodsResponse>() {
        @Override
        public void onResponse(Call<FoodsResponse> call, Response<FoodsResponse> response) {

            foodList = response.body().getFoods();
            adapter = new FoodsAdapter(getActivity(), foodList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<FoodsResponse> call, Throwable t) {

        }
    });

}

}

FoodsAdapter kelas publik memperluas RecyclerView.Adapter mengimplementasikan View.OnClickListener{

private Context mCtx;
private List<Food> foodList;

public FoodsAdapter(Context mCtx, List<Food> foodList) {
    this.mCtx = mCtx;
    this.foodList = foodList;
}

@NonNull
@Override
public FoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_foods, parent, false);
    return new FoodsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull FoodsViewHolder holder, int position) {
    Food food = foodList.get(position);

    holder.textViewFName.setText(food.getFName());
    holder.textViewUnitPrice.setText(String.format("%d", food.getUnitPrice()));
    holder.textViewAvailCount.setText(String.format("%d", food.getAvailCount()));
}

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


//////////////
@Override
public void onClick(View view) {
    Intent intent = new Intent(this.mCtx, AddOrderActivity.class);
    this.mCtx.startActivity(intent);
}
/////////////////

class FoodsViewHolder extends RecyclerView.ViewHolder {

    TextView textViewFName, textViewUnitPrice, textViewAvailCount;

    public FoodsViewHolder(View itemView) {
        super(itemView);

        textViewFName = itemView.findViewById(R.id.textViewFName);
        textViewUnitPrice = itemView.findViewById(R.id.textViewUnitPrice);
        textViewAvailCount = itemView.findViewById(R.id.textViewAvailCount);
    }
}

}

Makanan kelas umum {

private int fid, unitprice, availcount;
private String fname;

public Food(int fid, String fname, int unitprice, int availcount) {
    this.fid = fid;
    this.fname = fname;
    this.unitprice = unitprice;
    this.availcount = availcount;

}

public int getFId() {
    return fid;
}

public String getFName() {
    return fname;
}

public int getUnitPrice() {
    return unitprice;
}

public int getAvailCount() {
    return availcount;
}

}

-4
Ehsan Jkr 5 Agustus 2019, 17:50

1 menjawab

Jawaban Terbaik

Di kelas adaptor Anda di dalam fungsi onBindViewHolder tambahkan kode ini

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context,NoteActivity.class);
                intent.putExtra("note_id", currentNote.getNoteId());
                intent.putExtra("note_name",currentNote.getNoteName());
                intent.putExtra("note_teacher",currentNote.getNoteTeacher());
                intent.putExtra("note_university",currentNote.getNoteUniversity());
                intent.putExtra("note_image",currentNote.getNoteImage());
                intent.putExtra("note_is_downloaded", currentNote.getIsDownloaded());
                intent.putExtra("note_storage_path", currentNote.getStoragePath());
                intent.putExtra("note_download_link", currentNote.getNoteDownloadLink());
                context.startActivity(intent);

            }
        });
0
Mr.A2 5 Agustus 2019, 15:11