我正在努力使OnClickListener用于每个项目,并在每一行中获取数据。

我需要的是获取我单击的每一行的名称,但是,o只能获得该位置。有人可以帮我吗?

这是显示我需要的名字的屏幕截图。

我需要在此处输入NAME,但是,我只能获取该行的位置。我该如何取每一行的名字?

这是我的密码

UserViewHolder.class

package br.sosqueen.com.sosqueen;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static java.security.AccessController.getContext;

/**
 * Created by calvin on 15/04/2018.
 */

public class UsersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    View mView;
    Context mContext;

    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mContext = itemView.getContext();
        itemView.setOnClickListener(this);
    }

    public void bind(User user) {
        TextView user_name = (TextView) mView.findViewById(R.id.name_text);
        TextView user_status = (TextView) mView.findViewById(R.id.status_text);

        user_name.setText(user.getUsername());
        user_status.setText(user.getStatus());
    }

    @Override
    public void onClick(View view) {
        Log.d("viewholder", " " + getLayoutPosition());
    }
}

SearchFragment.java
package br.sosqueen.com.sosqueen;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


public class SearchFragment extends android.support.v4.app.Fragment {

    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mResultList;
    private DatabaseReference mUserDatabase;

    public static SearchFragment newInstance() { return new SearchFragment();  }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        mSearchField = (EditText) view.findViewById(R.id.search_field);
        mSearchBtn = (ImageButton) view.findViewById(R.id.search_btn);

        mResultList = (RecyclerView) view.findViewById(R.id.result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(mResultList.getContext()));

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchText = mSearchField.getText().toString();
                firebaseUserSearch(searchText);
            }
        });

        mUserDatabase = FirebaseDatabase.getInstance().getReference("usuarios");
        return view;
    }

    public void firebaseUserSearch(String searchText) {
        Toast.makeText(getContext(), "Started Search", Toast.LENGTH_LONG).show();

        Query firebaseSearchQuery = mUserDatabase.orderByChild("username").startAt(searchText).endAt(searchText + "\uf8ff");
        FirebaseRecyclerOptions<User> options =
                new FirebaseRecyclerOptions.Builder<User>()
                        .setQuery(firebaseSearchQuery, User.class)
                        .build();

        FirebaseRecyclerAdapter<User, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UsersViewHolder>(options) {
            @Override
            public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                Log.d("debugaoBuild","Antes de inflar");
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);
                Log.d("debugaoBuild","inflou");
                return new UsersViewHolder(v);
            }

            @Override
            protected void onBindViewHolder(UsersViewHolder holder, int position, User model) {
                holder.bind(model);
            }
        };
        firebaseRecyclerAdapter.startListening();
        mResultList.setAdapter(firebaseRecyclerAdapter);
    }
}

最佳答案

您可以将点击侦听器的设置移动到onBindViewHolder:

    protected void onBindViewHolder(UsersViewHolder holder, int position, User model) {
                    holder.bind(model);
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                         public void onClick(View v) {
                             Log.d("viewholder", " " + holder.getAdapterPosition());
                             Log.d("viewholder", " " + model.getUsername());

                         }
                    });
     }

07-24 14:55