gpt4 book ai didi

java - 如何使用 firebase 搜索查询结果项实现 OnClick 项

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:02 25 4
gpt4 key购买 nike

我已经创建了一个 firebase 搜索查询并且它运行良好。但现在我似乎无法弄清楚如何执行 onlick 和获取项目位置。以下是我的 firebase 搜索 Activity :

mUserDatabase=FirebaseDatabase.getInstance().getReference("Users").child("UserProfile";
mSearchField = findViewById(R.id.search_field);
mSearchBtn = findViewById(R.id.search_btn);
mResultList = findViewById(R.id.result_list);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));
mSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String searchText = mSearchField.getText().toString();
firebaseUserSearch(searchText);
}
});
}
private void firebaseUserSearch(String searchText) {
Toast.makeText(SearchActivity.this, "Searching......", Toast.LENGTH_LONG).show();

Query firebaseSearchQuery = mUserDatabase.orderByChild("profession").startAt(searchText).endAt(searchText + "\uf8ff");

FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.search_list_layout,
UsersViewHolder.class,
firebaseSearchQuery
) {
@Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {

viewHolder.setDetails(getApplicationContext(), model.getUsername(), model.getSummary(), model.getImageurl());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
private TextView user_name;
private TextView user_summary;
private ImageView user_image;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}

public void setDetails(Context ctx, String userName,String userSummary,String userImage){
user_name = mView.findViewById(R.id.name_text);
user_summary = mView.findViewById(R.id.summary_text);
user_image = mView.findViewById(R.id.profile_image);
user_name.setText(userName);
user_summary.setText(userSummary);
Glide.with(ctx).load(userImage).into(user_image);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
Snackbar.make(v, "Hello,How may i help you", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
}

当单击搜索结果中的单个项目时,如何在详细信息 Activity 中显示内容。

根据 Parag Pawar 的建议,我尝试了以下方法,但它不起作用>

       @Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {

viewHolder.setDetails(getApplicationContext(), model.getUsername(), model.getSummary(), model.getImageurl());
final String each_key = getRef(position).getKey();


}

};

然后在我的 UserViewHolder 中我初始化了 onclick:

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

Intent pDetail = new Intent(SearchActivity.this, ProfileDetail.class);
Users each_key = each_key.get(getAdapterPosition());
pDetail.putExtra(ProfileDetail.EXTRA_POSITION, each_key.getUserId());
startActivity(pDetail);


}
});

但我收到错误消息variable each_key might not have been initialized

根据 Parag Pawar 的最高建议修正了我的错误,并在 populateView`holder 中实现了他的 onclick,如下所示:

 viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Here use intent.putExtra() to send each_key on other activity and fetch data there*/
Toast.makeText(SearchActivity.this, "u clicked......" +position, Toast.LENGTH_LONG).show();
Intent each_key = new Intent(SearchActivity.this, ProfileDetail.class);
Users user = mResultList.get(position);
each_key.putExtra(ProfileDetail.EXTRA_POSITION, user.getUserId());
startActivity(each_key);



} });

但我在这里遇到错误:

 Users user = mResultList.get(position);

无法解析方法“get(int)”

mReultList 是我的 Recyclerview 的名字,我初始化如下:

private RecyclerView mResultList;     

但我不知道我这样做是否正确。我知道我应该将结果附加到适配器,但我不知道该怎么做。所以我尝试了:

  listItems = new ArrayList<>();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UsersList>(listItems,this);

但这似乎也行不通

更新代码

我创建了一个数组:

 private List<UsersList> listItems;//UsersList is my modal class

然后将其添加到我的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listItems = new ArrayList<>();

然后我使用这个数组如下实现了 putExtra:

Intent each_key = new Intent(TestSearch.this, ProfileDetail.class);
UsersList user = listItems.get(position);
//Here use intent.putExtra() to send each_key on other activity and fetch data there
Toast.makeText(TestSearch.this, "u clicked......" +position, Toast.LENGTH_LONG).show();
each_key.putExtra(ProfileDetail.EXTRA_POSITION, user.getUserId());
startActivity(each_key);

但是当我点击一个项目时出现这个错误:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.ecard.ecard.search.TestSearch$2$1.onClick(TestSearch.java:94)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

要从每个项目中获取键,在 populateViewholder 中执行示例:-

final String each_key = getRef(position).getKey();

要在每个项目上设置点击监听器,在 populateViewholder 中执行

viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
@Override
public void onClick(View v) {
/*Here use intent.putExtra() to send each_key on other activity and fetch data there*/

} }); }

关于java - 如何使用 firebase 搜索查询结果项实现 OnClick 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com