getAdapterPosition没有执行应做的工作

时间:2018-11-06 02:12:55

标签: java android filter android-recyclerview recycler-adapter

我有一个RecyclerView片段,我在编辑文本中添加了“过滤器搜索”。它可以正常工作,但是当我单击“回收卡”时,它会显示错误的详细信息。我最好的猜测是,它在getAdapterPosition中的位置不正确,因为假设我有此列表{a,b,c,d,e,f,g,h}。当我过滤并获得两个像{d,g}这样的主题时。如果单击d,它将重定向到a,如果单击g,它将重定向到b。

这是我的RecyclerView

    public class RecyclerProfile extends Fragment implements 
Adapter.AdapterListener,  com.example.cake.profiling.Adapter.SearchListener 
{

private RecyclerListener recyclerListener;
private List<Profile> profiles =  (new DAOProfile()).getProfile();
private Adapter recyclerAdapter= new Adapter(profiles, this);

public RecyclerProfile() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_recycler_profile, 
    container, false);

    RecyclerView recyclerView = view.findViewById(R.id.recyclerProfile);


    EditText editText = view.findViewById(R.id.editText);
    editText.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) {
            filter(s.toString());
        }
    });
    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager layoutManager = new 
    LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);


    recyclerView.setAdapter(recyclerAdapter);
    setHasOptionsMenu(true);

    return view;
 }



 private void filter(String text){
    ArrayList<Profile> filteredList = new ArrayList<>();
    for (Profile profile: profiles){
        if (profile.getName().toLowerCase().contains(text.toLowerCase())){
            filteredList.add(profile);
        }
    }
    recyclerAdapter.filterList(filteredList);
    profiles = new ArrayList<>(filteredList);
}

    @Override
    public void listen(Profile profile, Integer position) {
        recyclerListener.send(profile, position);
    }

    @Override
    public void profileSelected(Profile profile) {

    }

    //INTERFACE
    public interface RecyclerListener {
        void send(Profile profile, Integer position);
    }

    //ON ATTACH

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.recyclerListener = (RecyclerListener) context;
        }
    }

这是我的适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ProfileViewHolder> {

private List<Profile> profiles;
private AdapterListener adapterListener;


//CONSTRUCTOR


public Adapter(List<Profile> profiles,AdapterListener adapterListener) {

    this.profiles = profiles;
    this.adapterListener = adapterListener;
}

@NonNull
@Override
public Adapter.ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.card_profile, parent, false);
    ProfileViewHolder profileViewHolder = new ProfileViewHolder(view);
    return profileViewHolder;
}

@Override
public void onBindViewHolder(@NonNull Adapter.ProfileViewHolder holder, int position) {
    Profile profile = profiles.get(position);
    holder.setter(profile);
}

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

public void filterList (ArrayList<Profile> filteredList){
    profiles = filteredList;
    notifyDataSetChanged();


}




//VIEWHOLDER
class ProfileViewHolder extends RecyclerView.ViewHolder{

    private ImageView image;
    private TextView name;

    public ProfileViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.nameProfile);
        image = itemView.findViewById(R.id.imageProfile);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Profile profile = profile.get(getAdapterPosition());
               adapterListener.receive(profile, getAdapterPosition());


            }
        });


    }


    public void setter (Profile profile){
        name.setText(profile.getName());
        image.setImageResource(profile.getImage());
    }
}
public interface AdapterListener {
    void receive(Profile profile, Integer position);
 }
}

1 个答案:

答案 0 :(得分:0)

getAdapterPosition() 

这将返回当前数据集项目的位置。这就是我要做的

class ProfileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageView image;
    private TextView name;

    Profile mProfile;

    public ProfileViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.nameProfile);
        image = itemView.findViewById(R.id.imageProfile);
        name.setOnClickListener(this);
        image.setOnClickListener(this);
    }


    public void setter (Profile profile){
        if(profile != null) {
            mProfile = profile;
            name.setText(mProfile.getName());
            image.setImageResource(mProfile.getImage());
         }
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.item_nameProfile:
        case R.id.item_imageProfile:
            for(int i = 0 ; i < profiles.size() ; i++) {
                if(profiles.get(i).getName().equalsIgnoreCase(mProfile.getName()) {
                    adapterListener.receive(profiles.get(i), i);
                    break;
                }
            }
            break;
    }
}

public interface AdapterListener {
    void receive(Profile profile, Integer position);
}
}