当我点击recyclerview上的项目时如何获得密钥?

时间:2018-03-19 07:49:29

标签: android firebase search firebase-realtime-database android-recyclerview

我正在使用搜索和回收站视图创建应用程序。

这是我的search_adapter:

public class search_adapter extends RecyclerView.Adapter<search_adapter.SearchViewHolder> {

    private Context context;
    private DatabaseReference mDatabase;
    ArrayList<String> fullNameList;
    ArrayList<String> dDateList;
    private OnItemClickListener  mListener;
    Snapshot snapshot;

    public search_adapter(Context context, ArrayList<String> fullNameList, ArrayList<String> dDateList) {
        this.context = context;
        this.fullNameList = fullNameList;
        this.dDateList = dDateList;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    public  void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }
    public static class SearchViewHolder extends RecyclerView.ViewHolder{
        CardView clickCard;
        TextView full_Name;
        TextView death_Date;

        public SearchViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);
             full_Name = (TextView) itemView.findViewById(R.id.person_name);
            death_Date = (TextView) itemView.findViewById(R.id.person_death);

            clickCard = (CardView) itemView.findViewById(R.id.cardClick);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener !=null){
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION){
                            listener.onItemClick(position);
                        }
                    }
                }
            });

        }
    }
    @Override
    public search_adapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater mInflater = LayoutInflater.from(context);
         view = mInflater.inflate(R.layout.recyclerview_adapter,parent,false);
        return new search_adapter.SearchViewHolder(view, mListener);
    }

    public void onBindViewHolder(SearchViewHolder holder, final int position) {
        holder.full_Name.setText(fullNameList.get(position));
        holder.death_Date.setText(dDateList.get(position));
    }
    @Override
    public int getItemCount() {
        return fullNameList.size();
    }

在本课程中,我使用onclick监听器为Recycler视图创建视图 我还包括一个界面,以便我可以传递所点击项目的位置。

这是我的search_activity类:

public class SearchActivity extends AppCompatActivity implements search_adapter.OnItemClickListener{

    private RecyclerView mPerson;
    private EditText mFindField;
    public DatabaseReference mDatabase;
    ArrayList<String> fullNameList;
    ArrayList<String> dDateList;
    private search_adapter searchAdapter;
    int position = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);


        //Database Access//
        mDatabase = FirebaseDatabase.getInstance().getReference();

        //Recycler View//
        mPerson = (RecyclerView)findViewById(R.id.person_list);
        mPerson.setHasFixedSize(true);
        mPerson.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mPerson.setLayoutManager(new GridLayoutManager(this,3));
        mFindField = (EditText) findViewById(R.id.search_field);


        fullNameList = new ArrayList<>();
        dDateList = new ArrayList<>();


        mFindField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                        if (!editable.toString().isEmpty()){
                            setAdapter(editable.toString());
                        } else {
                            fullNameList.clear();
                            dDateList.clear();
                            mPerson.removeAllViews();
                        }
            }
        });
    }

    private void setAdapter(final String searchedString) {

            mDatabase.child("person").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                fullNameList.clear();
                dDateList.clear();
                mPerson.removeAllViews();
                int counter = 0;
                for (DataSnapshot snapshot: dataSnapshot.getChildren()){
                    String full_name = snapshot.child("fullName").getValue(String.class);
                    String dDate = snapshot.child("dDate").getValue(String.class);

                    if (full_name.toLowerCase().contains(searchedString.toLowerCase())){
                        fullNameList.add(full_name);
                        dDateList.add(dDate);
                        counter++;
                    } else if (dDate.toLowerCase().contains(searchedString.toLowerCase())){
                        fullNameList.add(full_name);
                        dDateList.add(dDate);
                        counter++;
                    }
                    if (counter == 15)
                        break;
                }
                searchAdapter = new search_adapter(SearchActivity.this, fullNameList, dDateList);
                mPerson.setAdapter(searchAdapter);

                searchAdapter.setOnItemClickListener(SearchActivity.this);


            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
    //new search//

    @Override
    public void onItemClick(int position) {

    }

这是通过的位置将去的地方, 但我的问题是,我不知道如何获得该传递位置的关键,我尝试使用getRef,就像其他人一样,但它不起作用。

我的目标是让用户点击回收站视图中的数据,打开新意图,并查看该特定点击数据的所有详细信息。
请帮忙。

1 个答案:

答案 0 :(得分:0)

您的适配器中有一个名称列表和一个日期列表,与生成和添加键列表的方式相同,在viewholder onBind方法中,您可以将键绑定到viewholder中的字段。而不是在点击监听器中发送位置,您可以发送密钥。