如何从 fire-base 检索数据到新活动中单击 RecyclerView

Posted

技术标签:

【中文标题】如何从 fire-base 检索数据到新活动中单击 RecyclerView【英文标题】:How to retrieve data from fire-base into a new activity On-click RecyclerView 【发布时间】:2020-06-02 00:23:11 【问题描述】:

这是一个从 firebase 检索图书数据的应用,我想知道如何将特定数据检索到新活动中。就像,在单击 RecyclerView 时检索特定的图书数据。我能够在 RecyclerView 上获得 onclick 位置,但我不知道如何将它与 firebase 一起使用

这是我的代码:

public class SearchPage extends AppCompatActivity 
    EditText searchbar;

    RecyclerView recyclerView;
    DatabaseReference reference;

    ArrayList <String> BookNameList;
    ArrayList <String> AuthorNameList;
    ArrayList <String> PicList;
    ArrayList <String> PublisherList;
    ArrayList <String> Shelfnols;
    ArrayList <String> Desc;
    SearchAdapter searchAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_page);

        searchbar = (EditText) findViewById(R.id.searchbar);
        reference = FirebaseDatabase.getInstance().getReference();
        reference.keepSynced(true);
        recyclerView = (RecyclerView) findViewById(R.id.rv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        BookNameList = new ArrayList <> ();
        PublisherList = new ArrayList <> ();
        AuthorNameList = new ArrayList <> ();
        Shelfnols = new ArrayList <> ();
        PicList = new ArrayList <> ();
        Desc = new ArrayList <> ();
        final ArrayList <String> uidlist = new ArrayList <String> ();




        searchbar.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 
                    BookNameList.clear();
                    AuthorNameList.clear();
                    PicList.clear();
                    PublisherList.clear();
                    Shelfnols.clear();
                    Desc.clear();
                    uidlist.clear();

                
            

            private void setAdapter(final String searchedString) 
                reference.child("books").addListenerForSingleValueEvent(new ValueEventListener() 
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) 

                        BookNameList.clear();
                        AuthorNameList.clear();
                        PicList.clear();
                        PublisherList.clear();
                        Shelfnols.clear();
                        Desc.clear();
                        uidlist.clear();


                        int counter = 0;
                        for (DataSnapshot snapshot: dataSnapshot.getChildren()) 
                            String uid = snapshot.getKey();

                            String desc = snapshot.child("Desc").getValue(String.class);
                            String bookname = snapshot.child("bookname").getValue(String.class);
                            String author = snapshot.child("author").getValue(String.class);
                            String image = snapshot.child("image").getValue(String.class);
                            String publisher = snapshot.child("Publisher").getValue(String.class);
                            String shelfno = snapshot.child("Shelf_no").getValue(String.class);
                            try 


                                if (bookname.toLowerCase().contains(searchedString.toLowerCase())) 
                                    BookNameList.add(bookname);
                                    AuthorNameList.add(author);
                                    PublisherList.add(publisher);
                                    Shelfnols.add(shelfno);
                                    PicList.add(image);
                                    Desc.add(desc);

                                    counter++;
                                 else if (author.toLowerCase().contains(searchedString.toLowerCase())) 
                                    BookNameList.add(bookname);
                                    AuthorNameList.add(author);
                                    PublisherList.add(publisher);
                                    Shelfnols.add(shelfno);
                                    PicList.add(image);
                                    Desc.add(desc);
                                    counter++;
                                
                             catch (Exception e) 
                                Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_LONG).show();
                            


                            if (counter == 15) 
                                break;
                            
                            SearchAdapter searchAdapter = new SearchAdapter(SearchPage.this, BookNameList, AuthorNameList, PicList, PublisherList, Shelfnols);
                            recyclerView.setAdapter(searchAdapter);
                        
                    

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) 

                    
                );
            
        );

    

这是我的适配器类

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> 
public static final String id="key";
    Context context;
    ArrayList<String> BookNameList;
    ArrayList<String> AuthorNameList;
ArrayList<String> PicList;
ArrayList<String> PublisherList;
ArrayList<String> Shelfnols;
LinearLayout booklayout;




class SearchViewHolder extends RecyclerView.ViewHolder
    ImageView bookimage;
    TextView bookname, authorname,publisher,shelfno;

    public SearchViewHolder(@NonNull View itemView) 
        super(itemView);
        bookimage =  itemView.findViewById(R.id.Bookimg);
        bookname =  itemView.findViewById(R.id.BookName);
        authorname =  itemView.findViewById(R.id.AuthorName);
        publisher =  itemView.findViewById(R.id.Publications);
        shelfno =  itemView.findViewById(R.id.Shelfno);
        booklayout=itemView.findViewById(R.id.LinLayout);
    


public SearchAdapter(Context context, ArrayList<String> bookNameList, ArrayList<String> authorNameList, ArrayList<String> picList,ArrayList<String> publisherList,ArrayList<String> shelfnols) 
    this.context = context;
    BookNameList = bookNameList;
    AuthorNameList = authorNameList;
    PicList = picList;
    PublisherList=publisherList;
    Shelfnols=shelfnols;



@NonNull
@Override
public SearchAdapter.SearchViewHolder  onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(context).inflate(R.layout.activity_search_layout,parent,false);
     return new SearchAdapter.SearchViewHolder(view);


@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, final int position) 
    holder.bookname.setText(BookNameList.get(position));
    holder.authorname.setText(AuthorNameList.get(position));
    holder.publisher.setText(PublisherList.get(position));
    holder.shelfno.setText(Shelfnols.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
                String t=String.valueOf(position);
            Toast.makeText(v.getContext(), "Item is clicked" + t, Toast.LENGTH_SHORT).show();

        
    );

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

       

   );

这是我希望显示图书详细信息的新活动

public class Bookdetailslayout extends SearchPage 

DatabaseReference reference;
TextView bookname,author,publisher,desc,location;
ImageView image;




@Override
protected void onCreate(Bundle savedInstanceState) 



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookdetailslayout);

    bookname = (TextView)findViewById(R.id.bkname);
    author = (TextView) findViewById(R.id.aname);
    publisher = (TextView) findViewById(R.id.pname);
    desc = (TextView) findViewById(R.id.bkdescription);
    location = (TextView) findViewById(R.id.bklocation);
    String s = getIntent().getStringExtra("key");


    DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference().child("books");
    databaseReference.addValueEventListener(new ValueEventListener() 
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
            Bookdeets bookdeets=dataSnapshot.getValue(Bookdeets.class);


            bookname.setText(bookdeets.getBookname());



        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) 

        
    );







    Glide.with(context).asBitmap().load(PicList.get(position)).placeholder(R.mipmap.ic_launcher_round).into(holder.bookimage);



@Override
public int getItemCount() 
    return BookNameList.size();

【问题讨论】:

看起来您在 Firebase 上获取数据,然后通过 ArrayList&lt;Object&gt; 传递到适配器。您可以使用position 获取您的数据onClickBookNameList.get(position).. 等 您需要将uid 传递给您的适配器,该适配器可用于从 Firebase 检索单个值 共享代码有什么问题? 请发布数据库结构以及您要发送意图的活动 @AlexMamo 此代码工作正常,但我一直在尝试将 onclick 侦听器设置为回收站视图,以转到将显示特定书籍详细信息的新活动 【参考方案1】:
      holder.itemView.setOnClickListener(new View.OnClickListener() 
       @Override
       public void onClick(View v) 
           Intent i = new Intent(context,BookdetailsActivity.class);
           i.putExtra("bookname",bookname);
           context.startActivity(i);
       
   );

上面的代码启动新活动以显示有关新活动的详细信息,同时传递唯一键以从 firebase 检索数据。

在 BookDetailsActivity 内 使用

获取键值

字符串名称 = getIntent().getStringExtra("bookname");

然后做你的事情以从 firebase 获取数据。

【讨论】:

我无法使用 bookname 它抛出一个错误,无法解决..我在我的适配器类的 onBindviewholder 下定义了它

以上是关于如何从 fire-base 检索数据到新活动中单击 RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章

如何从 onclick 侦听器单击的项目中检索数据

单击 Listview 中的项目时,如何在新活动中从 Firebase 检索数据?

如何从 fire-base 为不同的产品风格 Android 生成不同的 json 文件

ANDROID:从解析中检索图像到 android 活动

将“标题”项目保存到 ListView 并单击项目以将数据加载到新活动

如何在flutter中将标记从firebase(真实数据库)检索到新的google maps api