Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
Android 如何在过滤适配器后获得正确的位置?_Android - Fatal编程技术网

Android 如何在过滤适配器后获得正确的位置?

Android 如何在过滤适配器后获得正确的位置?,android,Android,请帮忙解决这个问题。当我按一下适配器的原样并显示正确的位置时,它可以正常工作。但是当我使用filter搜索值并单击过滤适配器时,结果显示的值与第一个位置相同,我得到的位置为1,但实际上我需要位置为9。如何在筛选适配器后获得数组项的正确位置 public class SearchActivity extends AppCompatActivity { private ArrayList<ExampleItem> mExampleList; public Recycl

请帮忙解决这个问题。当我按一下适配器的原样并显示正确的位置时,它可以正常工作。但是当我使用filter搜索值并单击过滤适配器时,结果显示的值与第一个位置相同,我得到的位置为1,但实际上我需要位置为9。如何在筛选适配器后获得数组项的正确位置

public class SearchActivity extends AppCompatActivity {

    private ArrayList<ExampleItem> mExampleList;
    public  RecyclerView mRecyclerView;
    private ExampleAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private final static String TAG = "MyActivity";
    public View view;
    public String value;
    public EditText editText;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        View row_content = getLayoutInflater().inflate(R.layout.example_item, null);
        view  = (View) row_content.findViewById(R.id.view);
         editText = findViewById(R.id.edittext);


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();

        value = editText.getText().toString();



        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(true);
        }

        createExampleList();
        buildRecyclerView();




        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());

            }
        });


    }

    private void filter(String text) {
        ArrayList<ExampleItem> filteredList = new ArrayList<>();

        for (ExampleItem item : mExampleList) {

            if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
                filteredList.add(item);
            }
        }

        mAdapter.filterList(filteredList);


    }

    private void createExampleList() {

        mExampleList = new ArrayList<>();
        mExampleList.add(new ExampleItem(R.drawable.us, "English"));
        mExampleList.add(new ExampleItem(R.drawable.chi, "简体中文"));
        mExampleList.add(new ExampleItem(R.drawable.chi, "繁體中文"));
        mExampleList.add(new ExampleItem(R.drawable.es, "Esperanto"));
        mExampleList.add(new ExampleItem(R.drawable.fr, "français"));
        mExampleList.add(new ExampleItem(R.drawable.it, "Italiano"));
        mExampleList.add(new ExampleItem(R.drawable.jp, "日本語"));
        mExampleList.add(new ExampleItem(R.drawable.kr, "한국어"));
        mExampleList.add(new ExampleItem(R.drawable.ru, "русский"));
        mExampleList.add(new ExampleItem(R.drawable.esp, "Español"));
        mExampleList.add(new ExampleItem(R.drawable.pt, "Português"));
        mExampleList.add(new ExampleItem(R.drawable.unar, "العربية"));
        mExampleList.add(new ExampleItem(R.drawable.bd, "বাংলা"));
        mExampleList.add(new ExampleItem(R.drawable.in, "हिन्दी"));
        mExampleList.add(new ExampleItem(R.drawable.de, "Deutsch"));
        mExampleList.add(new ExampleItem(R.drawable.za, "Afrikaans"));
        mExampleList.add(new ExampleItem(R.drawable.al, "Shqip"));
        mExampleList.add(new ExampleItem(R.drawable.et, "አማርኛ"));
        mExampleList.add(new ExampleItem(R.drawable.am, "Հայերեն"));
        mExampleList.add(new ExampleItem(R.drawable.az, "azərbaycan dili"));
        mExampleList.add(new ExampleItem(R.drawable.es, "euskara"));
        mExampleList.add(new ExampleItem(R.drawable.by, "беларуская мова"));
        mExampleList.add(new ExampleItem(R.drawable.ba, "bosanski jezik"));
        mExampleList.add(new ExampleItem(R.drawable.bs, "български език"));
        mExampleList.add(new ExampleItem(R.drawable.ad, "català"));
        mExampleList.add(new ExampleItem(R.drawable.ca, "нохчийн мотт"));
        mExampleList.add(new ExampleItem(R.drawable.cor, "corsu"));
        mExampleList.add(new ExampleItem(R.drawable.hr, "hrvatski jezik"));
        mExampleList.add(new ExampleItem(R.drawable.cz, "čeština"));
        mExampleList.add(new ExampleItem(R.drawable.dk, "dansk"));
        mExampleList.add(new ExampleItem(R.drawable.nl, "Nederlands"));
        mExampleList.add(new ExampleItem(R.drawable.ee, "eesti"));
        mExampleList.add(new ExampleItem(R.drawable.fl, "suomi"));
        mExampleList.add(new ExampleItem(R.drawable.nl, "Frysk"));
        mExampleList.add(new ExampleItem(R.drawable.ga, "Galego"));
        mExampleList.add(new ExampleItem(R.drawable.ge, "ქართული"));
        mExampleList.add(new ExampleItem(R.drawable.in, "ગુજરાતી"));


    }

    private void buildRecyclerView() {

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(mExampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {



                String string =  mExampleList.get(position).getText1();
                int img= mExampleList.get(position).getImageResource();
                Log.e(TAG,"LANGUAGE IS: " + string);



                Intent intent = new Intent(SearchActivity.this, MainActivity.class);
                intent.putExtra("NAME", string);
                intent.putExtra("POSITION",Integer.toString(position));
                intent.putExtra("PHOTO", img);
                startActivity(intent);





            }
        });

    }


}
公共类SearchActivity扩展了AppCompatActivity{
私有ArrayList mExampleList;
公共回收视图MRECYCLERVIW;
私有示例适配器mAdapter;
private RecyclerView.LayoutManager MLLayoutManager;
私有最终静态字符串TAG=“MyActivity”;
公众观点;
公共字符串值;
公共编辑文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u search);
查看行内容=GetLayoutFlater()。充气(R.layout.example_项,null);
view=(view)row_content.findViewById(R.id.view);
editText=findViewById(R.id.editText);
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
ActionBar ActionBar=getSupportActionBar();
value=editText.getText().toString();
if(actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
createExampleList();
buildRecyclerView();
editText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
过滤器(s.toString());
}
});
}
专用无效过滤器(字符串文本){
ArrayList filteredList=新建ArrayList();
例如(示例项:mExampleList){
if(item.getText1().toLowerCase().contains(text.toLowerCase())){
filteredList.add(项目);
}
}
mAdapter.filterList(filterList);
}
私有void createExampleList(){
mExampleList=newArrayList();
添加(新的示例项(R.drawable.us,“英语”);
添加(新的示例项(R.drawable.chi,)简体中文"));
添加(新的示例项(R.drawable.chi,)繁體中文"));
添加(新的示例项(R.drawable.es,“世界语”);
添加(新的示例项(R.drawable.fr,“français”);
添加(新的示例项(R.drawable.it,“意大利语”);
添加(新的ExampleItem(R.drawable.jp)日本語"));
添加(新的示例项(R.drawable.kr,)한국어"));
mExampleList.add(新的示例项(R.drawable.ru,“БбСб”);
添加(新的示例项(R.drawable.esp,“Español”);
添加(新的示例项(R.drawable.pt,“葡萄牙”);
添加(新的示例项(R.drawable.unar,“R.drawable.unar”);
添加(新的ExampleItem(R.drawable.bd)বাংলা"));
添加(新的示例项(R.drawable.in,)हिन्दी"));
添加(新的示例项(R.drawable.de,“Deutsch”);
添加(新的示例项(R.drawable.za,“南非荷兰语”);
添加(新的示例项(R.drawable.al,“Shqip”);
添加(新的ExampleItem(R.drawable.et)አማርኛ"));
mExampleList.add(新的示例项(R.drawable.am,“Հ㗑եեեն”);
添加(新的示例项(R.drawable.az,“azərbaycan dili”);
添加(新的示例项(R.drawable.es,“euskara”);
mExampleList.add(新的示例项(R.drawable.by,“ббаааааааааааа;
添加(新的示例项(R.drawable.ba,“bosanski-jezik”);
mExampleList.add(新的示例项(R.drawable.bs,“бъббббззбзбзббб;
添加(新的ExampleItem(R.drawable.ad,“catla”);
添加(新的示例项(R.drawable.ca,“R.drawable.ca”);
添加(新的示例项(R.drawable.cor,“corsu”);
添加(新的示例项(R.drawable.hr,“hrvatski-jezik”);
添加(新的示例项(R.drawable.cz,“čeština”);
添加(新的示例项(R.drawable.dk,“dansk”);
添加(新的ExampleItem(R.drawable.nl,“Nederlands”);
添加(新的示例项(R.drawable.ee,“eesti”);
添加(新的示例项(R.drawable.fl,“suomi”);
添加(新的示例项(R.drawable.nl,“Frysk”);
添加(新的示例项(R.drawable.ga,“Galego”);
添加(新的示例项(R.drawable.ge,)ქართული"));
添加(新的示例项(R.drawable.in,)ગુજરાતી"));
}
私有void buildRecyclerView(){
mRecyclerView=findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager=新的LinearLayoutManager(此);
mAdapter=新示例适配器(mExampleList);
mRecyclerView.setLayoutManager(mllayoutmanager);
mRecyclerView.setAdapter(mAdapter);
setOnItemClickListener(新示例Adapter.OnItemClickListener(){
@凌驾
公共空间单击(内部位置){
String String=mExampleList.get(position.getText1();
int img=mExampleList.get(position.getImageResource();
Log.e(标记“语言为:”+字符串);
意向意向=新意向(SearchActivity.this、MainActivity.class);
意图
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;


public interface OnItemClickListener{
    void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}


public static class ExampleViewHolder extends RecyclerView.ViewHolder {


    public ImageView mImageView;
    public TextView mTextView1;



    public ExampleViewHolder(View itemView, OnItemClickListener listener) {
        super(itemView);

        mImageView = itemView.findViewById(R.id.imageView);
        mTextView1 = itemView.findViewById(R.id.textView);

        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);

                       }
                   }

            }
        });


    }
}

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, mListener);
    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());



}

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

public void filterList(ArrayList<ExampleItem> filteredList) {
    mExampleList = filteredList;
    notifyDataSetChanged();
}
mAdapter = new ExampleAdapter(filteredList); //with filted list
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
private static class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

    /*   ADAPTER’S FUNCTIONS   */

    public ArrayList<ExampleItem> getDataList() {
        return mExampleList;
    }

}

private void buildRecyclerView() {

    /*   OTHER LOGICS HERE   */

    mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            // BELOW IS - YOUR CODE

    /*String string = mExampleList.get(position).getText1();
    int img = mExampleList.get(position).getImageResource();*/

            // THIS IS NEW WORKING CODE

            String string = mAdapter.getDataList().get(position).getText1();
            int img = mAdapter.getDataList().get(position).getImageResource();

            Log.d(TAG, "LANGUAGE IS: " + string);

            Log.d("NAME", string);
            Log.d("POSITION", Integer.toString(position));
            Log.d("PHOTO", String.valueOf(img));
        }
    });

}
package com.example.myapplication.java;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;


public class SearchActivity extends AppCompatActivity {

    private ArrayList<ExampleItem> mExampleList;
    public RecyclerView mRecyclerView;
    private ExampleAdapter mAdapter;
    private final static String TAG = "MyActivity";
    public String value;
    public EditText editText;

    static class ExampleItem {

        private int image;
        private String text1;

        ExampleItem(int image, String text1) {
            this.image = image;
            this.text1 = text1;
        }

        int getImageResource() {
            return image;
        }

        String getText1() {
            return text1;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        editText = findViewById(R.id.editText);
        value = editText.getText().toString();

        createExampleList();
        buildRecyclerView();

        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());
            }
        });
    }

    private void filter(String text) {
        ArrayList<ExampleItem> filteredList = new ArrayList<>();

        for (ExampleItem item : mExampleList) {

            if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
                filteredList.add(item);
            }
        }

        mAdapter.filterList(filteredList);
    }

    private void createExampleList() {

        mExampleList = new ArrayList<>();
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "English"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "简体中文"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "繁體中文"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Esperanto"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "français"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Italiano"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "日本語"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "한국어"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "русский"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Español"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Português"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "العربية"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "বাংলা"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "हिन्दी"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Deutsch"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Afrikaans"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Shqip"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "አማርኛ"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Հայերեն"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "azərbaycan dili"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "euskara"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "беларуская мова"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "bosanski jezik"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "български език"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "català"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "нохчийн мотт"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "corsu"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "hrvatski jezik"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "čeština"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "dansk"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Nederlands"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "eesti"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "suomi"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Frysk"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Galego"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "ქართული"));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "ગુજરાતી"));
    }

    private void buildRecyclerView() {

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(mExampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                // BELOW IS - YOUR CODE

                /*String string = mExampleList.get(position).getText1();
                int img = mExampleList.get(position).getImageResource();*/

                // THIS IS NEW WORKING CODE

                String string = mAdapter.getDataList().get(position).getText1();
                int img = mAdapter.getDataList().get(position).getImageResource();

                Log.d(TAG, "LANGUAGE IS: " + string);

                Log.d("NAME", string);
                Log.d("POSITION", Integer.toString(position));
                Log.d("PHOTO", String.valueOf(img));
            }
        });
    }

    private static class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

        private ArrayList<ExampleItem> mExampleList;
        private OnItemClickListener mListener;

        public interface OnItemClickListener {
            void onItemClick(int position);
        }

        void setOnItemClickListener(OnItemClickListener listener) {
            mListener = listener;
        }

        static class ExampleViewHolder extends RecyclerView.ViewHolder {

            ImageView mImageView;
            TextView mTextView1;

            ExampleViewHolder(View itemView, final OnItemClickListener listener) {
                super(itemView);

                mImageView = itemView.findViewById(R.id.imageView);
                mTextView1 = itemView.findViewById(R.id.textView);

                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);
                            }
                        }
                    }
                });
            }
        }

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

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

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

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

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

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

        ArrayList<ExampleItem> getDataList() {
            return mExampleList;
        }
    }
}