Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用搜索筛选器应用搜索时,RecyclerView获取错误位置_Java_Android - Fatal编程技术网

Java 使用搜索筛选器应用搜索时,RecyclerView获取错误位置

Java 使用搜索筛选器应用搜索时,RecyclerView获取错误位置,java,android,Java,Android,我使用RecyclerView,并在其上实现了搜索过滤器,我在这里面临一个问题 在我搜索后,当我按下项目时,我得到了错误的位置,而不是一个我搜索过的位置,请帮助我解决这个问题 例如,项目编号2在搜索Android后命名Android位置进入编号1。。为什么会有这个问题。。多谢各位 MyAdapter.java 滤器 TextWatcher mTextWatcher = new TextWatcher() { @Override public void bef

我使用RecyclerView,并在其上实现了搜索过滤器,我在这里面临一个问题

在我搜索后,当我按下项目时,我得到了错误的位置,而不是一个我搜索过的位置,请帮助我解决这个问题

例如,项目编号2在搜索Android后命名Android位置进入编号1。。为什么会有这个问题。。多谢各位

MyAdapter.java

滤器

   TextWatcher mTextWatcher = 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) {
            if (s.toString().equals("")){
                initAdapter();
            } else {
                searchItem(s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private void initAdapter(){
        adapter.listItems.clear();
        for (int i = 0; i < 10; i++) {
            listItems.add(new RecyclerItem("Item " + (i + 1), "Welcome to Torisan channel, this is description of item " + (i+1)));
        }
        adapter.notifyDataSetChanged();
    }

    private void searchItem(String itemname){
        int resultCount = 0;
        adapter.listItems.clear();
        for (int i = 0; i < tempListItems.size(); i++){
            if (tempListItems.get(i).getTitle().contains(itemname)){
                listItems.add(new RecyclerItem("Item " + (i + 1), "Welcome to Torisan channel, this is description of item " + (i+1)));
                resultCount ++;
            }
        }
        if (resultCount == 0){
            showToast();
        }
        adapter.notifyDataSetChanged();
    }

    public void showToast() {
        // Set the toast and duration
        int toastDurationInMilliSeconds = 1000;
        mToastToShow = Toast.makeText(this, "No results found.", Toast.LENGTH_LONG);

        // Set the countdown to display the toast
        CountDownTimer toastCountDown;
        toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
            public void onTick(long millisUntilFinished) {
                mToastToShow.show();
            }
            public void onFinish() {
                mToastToShow.cancel();
            }
        };

        // Show the toast and starts the countdown
        mToastToShow.show();
        toastCountDown.start();
    }
}
请用这个

for (int i = 0; i < tempListItems.size(); i++) {
    if (listItems.get(getAdapterPosition()).getName().equals(tempListItems.get(i).getName())) {
        int selectedPos = i;  //SelectedPos is that index
        break;
    }
}

你的过滤方法在哪里?这是因为当你过滤你的列表时,会创建一个新的列表,其中Android项目是第一位的。。请分享过滤方法。这样我们可以帮助你更多。ThanksIt没有给你错误的位置。这是因为每次搜索后,适配器列表都会根据您的筛选器进行重置。请不要通过破坏您的帖子为人们做更多的工作。通过在Stack Exchange SE网络上发布,您已根据授予SE分发该内容的不可撤销权利,即无论您未来的选择如何。根据SE政策,该帖子的非故意破坏版本是发布的版本。因此,任何故意破坏行为都将恢复原状。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    public List<RecyclerItem> listItems;
    private Context mContext;


    public MyAdapter(List<RecyclerItem> listItems, Context mContext) {
        this.listItems = listItems;
        this.mContext = mContext;


    }
    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {

        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setTag(getAdapterPosition());

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               final int position =    Integer.parse(v.getTag().toString());
                if (position == 0) {


                    Toast.makeText(mContext,"iOS",Toast.LENGTH_SHORT).show();

                }
                if (position == 1) {

                    Toast.makeText(mContext,"Android",Toast.LENGTH_SHORT).show();

                }

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final RecyclerItem itemList = listItems.get(position);
        holder.txtTitle.setText(itemList.getTitle());
        holder.txtDescription.setText(itemList.getDescription());
        holder.txtOptionDigit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Display option menu
                PopupMenu popupMenu = new PopupMenu(mContext, holder.txtOptionDigit);
                popupMenu.inflate(R.menu.option_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                        switch (item.getItemId()) {
                            case R.id.mnu_item_save:
                                Toast.makeText(mContext, "Saved " + listItems.get(position).getTitle(), Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.mnu_item_delete:
                                //Delete item
                                Toast.makeText(mContext, "Deleted " + listItems.get(position).getTitle(), Toast.LENGTH_SHORT).show();
                                listItems.remove(position);
                                notifyDataSetChanged();
                                break;
                            default:
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private  Button buttoncalling;
        public TextView txtTitle;
        public TextView txtDescription;
        public TextView txtOptionDigit;
        public ViewHolder(View itemView) {
            super(itemView);
            txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            txtDescription = (TextView) itemView.findViewById(R.id.txtDescription);
            txtOptionDigit = (TextView) itemView.findViewById(R.id.txtOptionDigit);
            buttoncalling  = (Button) itemView.findViewById(R.id.bbbbbbbbbb);
            buttoncalling.setOnClickListener(this);


        }

        @Override
        public void onClick(View view) {

        }
    }
}
for (int i = 0; i < tempListItems.size(); i++) {
    if (listItems.get(getAdapterPosition()).getName().equals(tempListItems.get(i).getName())) {
        int selectedPos = i;  //SelectedPos is that index
        break;
    }
}