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

如何使用firebase搜索查询结果项实现OnClick项,第1张

概述我已经创建了一个firebase搜索查询,它运行得很完美.但现在我似乎无法弄清楚如何进行onlick并获取项目位置.以下是我的firebase搜索活动:mUserDatabase=FirebaseDatabase.getInstance().getReference("Users").child("UserProfile";mSearchField=findViewById(R.id.sear

我已经创建了一个firebase搜索查询,它运行得很完美.但现在我似乎无法弄清楚如何进行onlick并获取项目位置.以下是我的firebase搜索活动:

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的建议,我尝试了以下内容,但它不起作用>

       @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:

@OverrIDeprotected 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.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)

解决方法:

在包含您当前的活动中
要单击的项目:
在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:

@OverrIDepublic 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);    }

根据您的具体情况:
在您的主要活动中:

   @OverrIDepublic 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

总结

以上是内存溢出为你收集整理的如何使用firebase搜索查询结果项实现OnClick项全部内容,希望文章能够帮你解决如何使用firebase搜索查询结果项实现OnClick项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1104912.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存