【发布时间】:2016-12-08 23:44:41
【问题描述】:
继上一个问题之后,有人建议我使用 RecyclerView 在我的 android 应用程序中显示食谱列表。
我的 RecyclerView 工作正常(代码如下),我的 RecyclerView 的每一行都包含一个配方名称和一个用于查看完整配方的按钮。但是我不确定如何检查哪个按钮是正确的,以及如何根据单击的按钮显示配方数据。
当单击“查看”按钮之一时,我想启动 ViewRecipe 活动,如果可能的话,我想将该食谱的“recipeKey”发送到新活动,以便我可以从 Firebase 检索其数据,或者如果我可以在 mRecipes ArrayList 中获取该食谱的索引,然后将此索引和 mRecipesIds 传递给 ViewRecipe。然而,这是我第一次使用 RecylerView,我不确定如何获取这些值并将它们发送到新活动。
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class RecipeListActivity extends AppCompatActivity {
ArrayList<Contact> contacts;
private static final String TAG = "RecipeBook";
private DatabaseReference mRecipeReference;
ValueEventListener mRecipeListener;
RecipeAdapter adapter;
private RecyclerView mRecipeRecycler;
private ImageButton Back;
String recipeKey;
ArrayList<Recipe> recipeMap = new ArrayList<Recipe>();
RecyclerView rvContacts;
private TextView test;
String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
mRecipeRecycler = (RecyclerView) findViewById(R.id.rvRecipes);
mRecipeReference = FirebaseDatabase.getInstance().getReference().child("likedrecipe");
mRecipeRecycler.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onStart() {
super.onStart();
ValueEventListener recipeListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot recipeSnapshot: dataSnapshot.getChildren()) {
Recipe recipe = recipeSnapshot.getValue(Recipe.class);
recipeKey = recipeSnapshot.getKey();
recipe.id = recipeKey;
name = recipe.Name;
recipeMap.add(recipe);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mRecipeReference.addValueEventListener(recipeListener);
mRecipeListener = recipeListener;
// Listen for comments
adapter = new RecipeAdapter(this, mRecipeReference);
mRecipeRecycler.setAdapter(adapter);
}
@Override
public void onStop() {
super.onStop();
if (mRecipeListener!= null) {
mRecipeReference.removeEventListener(mRecipeListener);
}
//RecipeAdapter.cleanupListener();
}
public static class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView nameTextView;
public Button messageButton;
public RecipeViewHolder(View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
messageButton = (Button) itemView.findViewById(R.id.message_button);
messageButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == messageButton.getId()) {
//startActivity(new Intent(this, ViewRecipe.class));
}
}
}
public class RecipeAdapter extends RecyclerView.Adapter<RecipeViewHolder> {
private List<Recipe> mRecipes = new ArrayList<Recipe>();
private List<String> mRecipeIds = new ArrayList<String>();
private Context mContext;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
public RecipeAdapter(Context context, DatabaseReference ref) {
mContext = context;
mDatabaseReference = ref;
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
Recipe recipe = dataSnapshot.getValue(Recipe.class);
mRecipeIds.add(dataSnapshot.getKey());
mRecipes.add(recipe);
notifyItemInserted(mRecipes.size() - 1);
// [END_EXCLUDE]
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
Recipe newrecipe = dataSnapshot.getValue(Recipe.class);
String recipeKey = dataSnapshot.getKey();
int recipeIndex = mRecipeIds.indexOf(recipeKey);
if (recipeIndex > -1) {
mRecipes.set(recipeIndex, newrecipe);
notifyItemChanged(recipeIndex);
} else {
Log.w(TAG, "onChildChanged:unknown_child:" + recipeKey);
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
String recipeKey = dataSnapshot.getKey();
int recipeIndex = mRecipeIds.indexOf(recipeKey);
if (recipeIndex > -1) {
mRecipeIds.remove(recipeIndex);
mRecipes.remove(recipeIndex);
notifyItemRemoved(recipeIndex);
} else {
Log.w(TAG, "onChildRemoved:unknown_child:" + recipeKey);
}
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
Recipe movedRecipe = dataSnapshot.getValue(Recipe.class);
String recipeKey = dataSnapshot.getKey();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postRecipes:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load recipes.",
Toast.LENGTH_SHORT).show();
}
};
ref.addChildEventListener(childEventListener);
mChildEventListener = childEventListener;
}
@Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.item_recipe, parent, false);
return new RecipeViewHolder(view);
}
@Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
Recipe recipe = mRecipes.get(position);
holder.nameTextView.setText(recipe.Name);
holder.messageButton.setText("View");
}
@Override
public int getItemCount() {
return mRecipes.size();
}
public void cleanupListener() {
if (mChildEventListener != null) {
mDatabaseReference.removeEventListener(mChildEventListener);
}
}
}
}
【问题讨论】:
-
每一行都应该有一个与数据源相关的唯一 Id
标签: android firebase android-recyclerview firebase-realtime-database