【问题标题】:How do I use adapter position to retrieve data from firebase to a new activity如何使用适配器位置将数据从 firebase 检索到新活动
【发布时间】:2020-02-19 06:43:05
【问题描述】:

我从 firebase 搜索书籍的 SearchPage

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<>();







    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();


            }
        }

        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();



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


                       Log.i(uid,"ids");
                        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) {

                }




            });
        }
    });


}

这是我在单击时获取适配器位置的适配器类 SearchAdapter

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
final public String id="bookname";

    Context context;
    ArrayList<String> BookNameList;
    ArrayList<String> AuthorNameList;
ArrayList<String> PicList;
ArrayList<String> PublisherList;
ArrayList<String> Shelfnols;
LinearLayout booklayout;
DatabaseReference reference;





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);
        reference = FirebaseDatabase.getInstance().getReference();
        reference.keepSynced(true);

    }
}

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) {

                 Toast.makeText(context,"clicked "+position,Toast.LENGTH_LONG).show();
                   Intent i = new Intent(context,Bookdetailslayout.class);

                 context.startActivity(i);
               }
           });






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

}

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

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

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);
    image=(ImageView)findViewById(R.id.img) ;

    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 Bname=getIntent().getStringExtra("bookname");
    Log.i(Bname, "onCreate: ");



    DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference().child("books");



}

到目前为止,我能够将 onclick 设置为新活动并将点击的位置作为项目 1,2 等...我如何使用此位置将​​数据从 firebase 获取到新活动

【问题讨论】:

  • 你能添加你的数据库结构吗?
  • 您尝试检索数据的 book_id 在哪里?
  • @Ashish 我没有根据书名和作者姓名为每本被淘汰的书提供 bookid
  • 请发布数据库结构,我们可以帮助您

标签: java android firebase android-recyclerview adapter


【解决方案1】:

创建界面

public interface SetclickListener {

    public void  getposition(int position);
}

在适配器内部创建构造函数

 SetclickListener setclicklistener;
public SetclickListener getSetclick(Context context) {
        this.context=context;
        return setclicklistener;
    }

public void setclicklistener(SetclickListener setclickListener){
        setclicklistener=setclickListener;
    }

获取所选位置

 setclicklistener.getposition(getAdapterPosition());

然后您可以在 SearchPage 活动中实现,然后您可以使用 Bundle 传递选定的位置数据,您可以将数据发送到其他活动

@Override
    public void getposition(int position) {
                        Intent intent = new Intent(context,DetailsActivity.class);
                        intent.putExtra("name", bookNameList.get(position));
                        startActivity(intent);

                }

        }

}

【讨论】:

  • Durai 我没有看到任何模型,他在没有模型的情况下传递了所有数据。所以他需要通过arraylist来检索
  • 这样就可以得到BookNameList.get(position);
  • 是的,但他的数据可能在 bookid 下,所以我认为发送书名来检索不是正确的方法
  • 这里我仅以 BookNameList 为例。你只能思考逻辑。我们只能提供示例代码
【解决方案2】:

我在我的适配器类中使用它来获取位置

 public void onClick(View v) {


                   Intent i = new Intent(context,Bookdetailslayout.class);
                   i.putExtra("bookname",BookNameList.get(position));

                   i.putExtra("Image",PicList.get(position));
                   i.putExtra("author_name",AuthorNameList.get(position));
                   i.putExtra("publisher_name",PublisherList.get(position));
                   context.startActivity(i);
               }
           });

然后在下一个活动中

 String imgs=getIntent().getStringExtra("Image");
    String Bname=getIntent().getStringExtra("bookname");
    title.setText(Bname);
    String Author=getIntent().getStringExtra("author_name");
    author.setText(Author);
    String publisher=getIntent().getStringExtra("publisher_name");
    pub.setText(publisher);
    Toast.makeText(this,"image "+imgs,Toast.LENGTH_LONG).show();
    Picasso.get().load(imgs).into(image);

它的工作原理..感谢这里的所有帮助

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 2020-11-05
    • 2018-08-14
    • 2017-10-08
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多