我已经检索了从firestore
到RecyclerView
的数据,然后我需要点击我的recyclerView
并显示我点击新活动的数据。如何获取数据?
这是主要活动的代码
List<FoodModel>models;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_home);
db = FirebaseFirestore.getInstance();
getData();
}
public void getData(){
models = new ArrayList<>();
db.collection("Food").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (!documentSnapshots.isEmpty()){
for (DocumentSnapshot docSnap: documentSnapshots.getDocuments()) {
FoodModel model = docSnap.toObject(FoodModel.class);
models.add(model);
foodAdapter = new FoodAdapter(models, getApplicationContext(), new FoodAdapter.OnFoodClick() {
@Override
public void OnClick(FoodModel food) {
Intent intent = new Intent(MainActivity.this, FoodActivity.class);
startActivity(intent);
}
});
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
这是适配器
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder>{
LayoutInflater inflater;
List<FoodModel> foodModels;
OnFoodClick listener;
Context context;
public FoodAdapter(List<FoodModel> foodModels, Context context, OnFoodClick listener) {
this.foodModels = foodModels;
this.context = context;
this.listener = listener;
inflater = LayoutInflater.from(context);
}
@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.view_holder_food, null, false);
FoodViewHolder holder = new FoodViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
FoodModel model = foodModels.get(position);
holder.tvName.setText(model.getName());
}
@Override
public int getItemCount() {
return foodModels.size();
}
public void addItem(FoodModel food){
foodModels.add(food);
notifyDataSetChanged();
}
public class FoodViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
public FoodViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getLayoutPosition();
listener.OnClick(foodModels.get(position));
}
});
}
}
public interface OnFoodClick{
public void OnClick(FoodModel food);
}
}
这是我的第二个活动。我想显示Firestore的名称,类型和价格。当我单击回收器视图时,数据不会显示在活动中。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
firedb = FirebaseFirestore.getInstance();
tvName = findViewById(R.id.tv_food_name);
tvType = findViewById(R.id.tv_type);
tvPrice = findViewById(R.id.tv_price);
getFoodData();
}
private void getFoodData(){
firedb.collection("Food").document().get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()){
loadData(documentSnapshot.toObject(FoodModel.class));
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
private void loadData(FoodModel foodModel){
tvName.setText(foodModel.getFoodName());
tvType.setText(foodModel.getType());
tvPrice.setText(foodModel.getPrice());
}
}
这是foodModel
public class FoodModel {
String foodName;
String type;
String price;
public FoodModel() {
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
答案 0 :(得分:1)
制作一个界面来处理像这样的回收者视图点击事件。 将下面的代码定义到适配器中。
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
// below code handle click event on recycler view item.
String data=mStringList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data);
}
});
}
然后在活动适配器绑定到recycleler视图后,然后调用此方法在Recycler视图上获取您的单击项目。
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
@Override
public void onClick(String str) {
Log.d("Click Value",str);
}
});