我遇到了一个问题。
所以我想从我的RecyclerView中点击的项目中获取id(可能是getKey()),将其传递给下一个活动。
我已经设置了适配器。我尝试了很多解决方案,但显然没有任何效果。 使用此解决方案,我从我的数据库中获得所有键。但我只想点击一个。
这是我的正常活动:
public class searchRecipeActivity extends AppCompatActivity {
EditText searchRecipe;
RecyclerView showList;
FirebaseUser user;
DatabaseReference ref;
FirebaseAuth firebaseAuth;
Context context;
ArrayList<String> durationList;
ArrayList<String> nameList;
//ArrayList<String> recipeList;
ArrayList<String> ingList;
SearchAdapterRecipe searchAdapterRecipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_recipe);
searchRecipe = (EditText) findViewById(R.id.search_txt);
showList = (RecyclerView) findViewById(R.id.listview_txt);
user = firebaseAuth.getInstance().getCurrentUser();
ref = FirebaseDatabase.getInstance().getReference();
durationList = new ArrayList<>();
nameList = new ArrayList<>();
ingList = new ArrayList<>();
showList.setHasFixedSize(true);
showList.setLayoutManager(new LinearLayoutManager(this));
showList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
searchRecipe.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().isEmpty()){
setAdapter(s.toString());
} else {
nameList.clear();
ingList.clear();
durationList.clear();
showList.removeAllViews();
}
}
});
}
private void setAdapter(final String searchedString) {
ref.child("recipes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
nameList.clear();
ingList.clear();
durationList.clear();
showList.removeAllViews();
int count = 0;
for (final DataSnapshot snapshot : dataSnapshot.getChildren()){
String uid = snapshot.getKey();
String name = snapshot.child("name").getValue(String.class);
String zutaten = snapshot.child("zutat").getValue(String.class);
String dauer = snapshot.child("dauer").getValue(String.class);
if (name.toLowerCase().contains(searchedString.toLowerCase())){
nameList.add(name);
ingList.add(zutaten);
durationList.add(dauer);
count++;
} else if (dauer.toLowerCase().contains(searchedString.toLowerCase())){
nameList.add(name);
ingList.add(zutaten);
durationList.add(dauer);
count++;
} else if (zutaten.toLowerCase().contains(searchedString.toLowerCase())){
nameList.add(name);
ingList.add(zutaten);
durationList.add(dauer);
count++;
}
if (count == 10){
break;
}
searchAdapterRecipe = new SearchAdapterRecipe(searchRecipeActivity.this, nameList, ingList, durationList);
showList.setAdapter(searchAdapterRecipe);
showList.addOnItemTouchListener(
new RecyclerItemClickListener(context, showList ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
String rID = snapshot.getKey();
//Syso
System.out.println("ID: " + rID);
Intent intent = new Intent(getBaseContext(), showRecipeActivity.class);
intent.putExtra("recipeID", rID);
startActivity(intent);
}
@Override public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
和我的适配器:
public class SearchAdapterRecipe extends RecyclerView.Adapter<SearchAdapterRecipe.SearchViewHolder> {
Context context;
ArrayList<String> durationList;
ArrayList<String> nameList;
ArrayList<String> ingList;
class SearchViewHolder extends RecyclerView.ViewHolder{
TextView name, zutat, dauer;
public SearchViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.recipeName);
zutat = (TextView) itemView.findViewById(R.id.recipeZutat);
dauer = (TextView) itemView.findViewById(R.id.recipeDauer);
}
}
public SearchAdapterRecipe(Context context, ArrayList<String> durationList, ArrayList<String> nameList, ArrayList<String> ingList) {
this.context = context;
this.durationList = durationList;
this.nameList = nameList;
this.ingList = ingList;
}
@Override
public SearchAdapterRecipe.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.search_list_item, parent, false);
return new SearchAdapterRecipe.SearchViewHolder(view);
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
holder.name.setText(nameList.get(position));
holder.zutat.setText(ingList.get(position));
holder.dauer.setText(durationList.get(position));
}
@Override
public int getItemCount() {
return nameList.size();
}
}
也许你可以帮助我。
谢谢!
答案 0 :(得分:0)
在addOnItemTouchListener
我会做
String rID = *yourList*.get(position).getKey();