我已经创建了一个firebase搜索查询,它运行得很完美。但是现在我似乎无法弄清楚如何进行onlick并获取项目位置。以下是我的火力搜索活动:
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();
}
});
}
如果点击搜索结果中的单个项目,我如何在详细信息活动中显示内容。
基于 Parag Pawar的建议,我尝试了以下操作,但它无效&gt;
@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);
}
});
但我收到错误 变量each_key可能尚未初始化
根据 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)
答案 0 :(得分:2)
要从每个项目中获取密钥,请在populateViewholder中执行 示例: -
final String each_key = getRef(position).getKey();
要在每个项目上设置Click Listeners,请在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*/
} }); }
答案 1 :(得分:1)
我说两件事都在populateViewHolder()中 您没有初始化错误,因为您在Viewholder类中使用了each_key。
复制此
final String each_key = getRef(position).getKey();
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*/
} }); }
并将其粘贴到populateViewHolder方法
中答案 2 :(得分:1)
在包含当前活动的活动中 要单击的项目: 在populateVieHolder中创建一个要引用的String:
final String single_view = getRef(position).getKey();
然后将onclick附加到ViewHolder:
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
在您的 OnClick 中,您的意图和 putExtras :
@Override
public void onClick(View view) {
//give your intent a name to reference from("moreDetail" in this case)
Intent moreDetail = new Intent(SearchActivity.this,MoreDetail.class);
//attach the intent reference to access each item and key
moreDetail.putExtra(MoreDetail.EXTRA_POSITION,single_view);
startActivity(moreDetail);//Start the new activity
然后在您的更多细节活动中: 添加ExtraPosition字符串
String single_view = getIntent().getExtras().getString(MoreDetail.EXTRA_POSITION);//get extra position reference previous view
从此处开始,您需要显示来自firebase的数据
从Firebase访问单个值
firebaseRef.child(single_view).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//For single items
// for example:String user_name = (String) dataSnapshot.child("username").getValue();
或强>
使用Listview访问附加到适配器的值
firebaseRef.child(single_view).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ListItem data = dataSnapshot.getValue(ListItem.class);
listItems.add(data);
adapter.notifyDataSetChanged();
//if you are accessing your data from a ListView
有很多方法可以做到这一点。另一种方法是在你的Recycler适配器中创建一个interface.declare onclick listener和contex:
OnItemClickListener callback;
private Context mContext;
然后为您的视图持有者实现Onclick侦听器
public static class UsersViewHolder extends RecyclerView.ViewHolder implements OnItemClickListener {
使用回调功能添加onclick以访问您的View Holder:
@Override
public void onClick(View v) {
callback.onItemClick(getAdapterPosition());
}
然后创建一个界面:
public interface OnItemClickListener {
void onItemClick(int pos);
}
根据您的具体情况: 在您的主要活动中:
@Override
public void onItemClick(int pos) {
Intent pDetail = new Intent(this.getContext(), ProfileDetail.class);//get the context
ListItem listItem = listItems.get(pos);//get the position
//Then your puEtxra here followed by start activity
startActivity(pDetail);
}
最后在详细信息视图中调用EXTRA_POSITION,然后调用数据快照。 您可以在github上的firebase文档和firebase快速入门示例中找到更多详细信息: GitHub FirebaseDocs