过滤后位置错误

时间:2018-02-15 18:51:04

标签: android

我的申请有问题。实际上,我完全陷入困境。我有一个已实施clicklistener和searchview的国家/地区列表。在我没有搜索列表的情况下单击给定元素的情况下,一切都正确显示并显示有关给定国家/地区的信息。但是当我搜索列表并单击元素时,应用程序会转到有关随机国家/地区的信息。我不知道为什么会这样。我没有错误。只有一个警告: W / IInputConnectionWrapper:非活动InputConnection上的finishComposingText

MainActivity

public static final String EXTRA_COUNTRY_NAME = "country_name";
public static final String EXTRA_COUNTRY_REGION = "country_region";
public static final String EXTRA_COUNTRY_NATIVE_NAME = "country_nativename";
public static final String EXTRA_COUNTRY_NAME_LANGUGES= "country_namelanguages";



private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<ModelJsona> dataArrayList;





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

}


private void initViews(){
    recyclerView=(RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    loadJSON();
}

private void loadJSON(){
    dataArrayList = new ArrayList<>();
    Retrofit retrofit=new Retrofit.Builder().baseUrl("https://restcountries.eu/").addConverterFactory(GsonConverterFactory.create()).build();
    RequestInterface requestInterface=retrofit.create(RequestInterface.class);
    Call<List<ModelJsona>> call= requestInterface.getJSON();

    call.enqueue(new Callback<List<ModelJsona>>() {


        @Override
        public void onResponse(Call<List<ModelJsona>> call, Response<List<ModelJsona>> response) {
            dataArrayList = response.body();
            dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
            recyclerView.setAdapter(dataAdapter);
            dataAdapter.setOnitemClickListener(MainActivity.this);

        }


        @Override
        public void onFailure(Call<List<ModelJsona>> call, Throwable t) {
            Log.e("Error",t.getMessage());
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu, menu);

    MenuItem search = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
    search(searchView);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}

private void search(SearchView searchView) {

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            dataAdapter.getFilter().filter(newText);
            if (dataAdapter !=null)dataAdapter.getFilter().filter(newText);
            return true;
        }
    });
}


@Override
public void onItemClick(int position) {
    Intent detailsIntent = new Intent(this,Details.class);
    ModelJsona click = dataArrayList.get(position);


    detailsIntent.putExtra(EXTRA_COUNTRY_NAME,click.getName());
    detailsIntent.putExtra(EXTRA_COUNTRY_REGION,click.getRegion());
    detailsIntent.putExtra(EXTRA_COUNTRY_NATIVE_NAME,click.getNativeName());
    detailsIntent.putExtra(EXTRA_COUNTRY_NAME_LANGUGES,click.getLanguages().get(0).getName());


    startActivity(detailsIntent);

}

}

DataAdapter的

  private Context context;
private List<ModelJsona> dataList;
private List<ModelJsona> filtr;
private OnitemClickListener mListener;


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

public void setOnitemClickListener (OnitemClickListener listener)
{
    mListener=listener;
}

public DataAdapter(Context context, List<ModelJsona> dataList) {
    this.context = context;
    this.dataList = dataList;
    this.filtr= dataList;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row, parent, false);
    return new MyViewHolder(view);
}

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

    holder.country_name.setText(filtr.get(position).getName());
    holder.country_region.setText(filtr.get(position).getRegion());
    holder.country_nativename.setText(filtr.get(position).getNativeName());
    holder.country_languagename.setText(filtr.get(position).getLanguages().get(0).getName());





}

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

}


public Filter getFilter() {

    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            String charString = charSequence.toString();

            if (charString.isEmpty()) {

                filtr = dataList;
            } else {

                ArrayList<ModelJsona> listadoprzeszukiwan = new ArrayList<>();

                for (ModelJsona wyszukiwarka : dataList) {

                    if (
                            wyszukiwarka.getName().contains(charString) ||
                            wyszukiwarka.getName().toLowerCase().contains(charString) ||
                            wyszukiwarka.getRegion().contains(charString) ||
                            wyszukiwarka.getRegion().toLowerCase().contains(charString) ||
                            wyszukiwarka.getNativeName().contains(charString) ||
                            wyszukiwarka.getNativeName().toLowerCase().contains(charString) ||
                            wyszukiwarka.getTopLevelDomain().contains(charString))


                             {

                        listadoprzeszukiwan.add(wyszukiwarka);
                    }
                }

                filtr = listadoprzeszukiwan;
            }

            FilterResults wynikiszukania = new FilterResults();
            wynikiszukania.values = filtr;
            return wynikiszukania;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults wynikiszukania) {
            filtr = (ArrayList<ModelJsona>) wynikiszukania.values;
            notifyDataSetChanged();
        }
    };
}


public class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView country_name, country_region,  country_languagename,country_nativename;


    public MyViewHolder(View itemView) {
        super(itemView);
        country_name = (TextView) itemView.findViewById(R.id.country_name);
        country_region = (TextView) itemView.findViewById(R.id.country_region);
        country_nativename = (TextView) itemView.findViewById(R.id.country_nativename);
        country_languagename= (TextView) itemView.findViewById(R.id.country_languagename);


    itemView.setOnClickListener((new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener !=null){
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    mListener.onItemClick(position);
                }
            }
        }
    }));


    }
}

}

1 个答案:

答案 0 :(得分:2)

我看到你在过滤器列表中得到的位置是指dataList中具有相同位置值的不同对象,因为过滤后的过滤器列表成为对结果的引用但仍然是dataList保存所有数据。

你可以用这个替换itemView点击列表器:

   itemView.setOnClickListener((new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mListener !=null){
            int position = getAdapterPosition();
            String name = filtr.get(position).getName();
            for (int i=0 ; i <dataList.size() ; i++ ){
            if(name.equals(dataList.get(i).getName()){
            position = i; 
            break;
            }
            }
            if (position != RecyclerView.NO_POSITION) {
                mListener.onItemClick(position);
            }
        }
    }
}));