简体   繁体   中英

My Method is not running as i expected

My Method is not running as i expected, when i click at top row, it shows correct, but when i search another key, and when i click it, it shows the same. what did i missed ? somehow its connect to arraylist [0,1,2,3,4,5] so, when i click top row its point to 0, and its always correct, How do i fix this? 在此处输入图片说明 在此处输入图片说明

this is my method:

private void buildRecyclerView(){
        // Create recycler view.
        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        // Create an adapter and supply the data to be displayed.
        mAdapter = new ExampleAdapter(mExampleList);
        // Give the recycler view a default layout manager.
        mRecyclerView.setLayoutManager(mLayoutManager);
        // Connect the adapter with the recycler view.
        mRecyclerView.setAdapter(mAdapter);
        //buat hilang dulu diawal
        mRecyclerView.setVisibility(View.GONE);

        mRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(this, mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        ExampleItem item = (ExampleItem) mExampleList.get(position);
                        if (item.getText1().equals("A12345")){
                            AlertDialog.Builder builder = new AlertDialog.Builder(menu_utama.this);
                            builder.setMessage("The text is A12345")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(menu_utama.this);
                            builder.setMessage("NOT THE SAME")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    }
                    @Override
                    public void onLongItemClick(View view, int position) {
                        // do whatever
                    }
                })
        );
    }

and this is the data :

private void createExampleList(){

        mExampleList = new ArrayList<>();
        mExampleList.add(new ExampleItem(R.drawable.folder_download, "A12345", "Kata-kata nya"));
        mExampleList.add(new ExampleItem(R.drawable.folder_download, "B54321", "Kata-kata nya"));
        mExampleList.add(new ExampleItem(R.drawable.folder_download, "C23456", "Kata-kata nya "));
        mExampleList.add(new ExampleItem(R.drawable.folder_download, "D65432", "Kata-kata nya"));
        mExampleList.add(new ExampleItem(R.drawable.folder_download, "E34567", "Kata-kata nya"));
    }

and this is the RecyclerItemClickListener :

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onLongItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

    @Override
    public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

and this is my adapter :

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>{

    private ArrayList<ExampleItem> mExampleList;

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {

        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;

        public ExampleViewHolder(View itemView ) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
        }

    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
        mExampleList = exampleList;
    }

    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
                parent, false);

        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);

        holder.mImageView.setImageResource(currentItem.getImageResource());
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
    }

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

    public void filterList(ArrayList<ExampleItem> filteredList) {
        mExampleList = filteredList;
        notifyDataSetChanged();
    }
}

When you filter the data, the clicked item position is not necessarily the position in the 'global' mExampleList, so the item you get is not really the item you click.

You need to add a method to your ExampleAdapter:

public ExampleItem getItemForPosition(int position) {
  return mExampleList.get(position); //This is the filtered list!
}

And in your 'onItemClick' callback use that method and not retrieve the data from the global mExampleList, like so:

ExampleItem item = mAdapter.getItemForPosition(position);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM