如何从回收视图Android获取过滤数据的新位置



我已经通过编辑文本在RecycleView中实现了搜索过滤器。我已经将onClickListener设置为列表。 当我通过editext搜索时,一切正常,数据显示完美。但是当我单击"过滤后的数据"时。它从旧列表中获取位置,例如 0。

我的适配器类

package com.gujja.ajay.coronovirus;
public class WorldDataAdapter extends RecyclerView.Adapter<WorldDataAdapter.WorldDataViewHolder> {
private final Context context;
private ArrayList<CovidCountry> covidCountries;
private OnItemClickListener covidListener;

public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
covidListener = listener;
}

public WorldDataAdapter(Context context, ArrayList<CovidCountry> covidCountries) {
this.covidCountries = covidCountries;
this.context = context;
}
@NonNull
@Override
public WorldDataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.activity_world_data_list, parent, false);
return new WorldDataViewHolder(view,covidListener);
}
@Override
public void onBindViewHolder(@NonNull WorldDataViewHolder holder, int position) {
CovidCountry covidCountry = covidCountries.get(position);
String countryFlagUrl = covidCountry.getmCovidCountryImages();
holder.worldDataTitle.setText(covidCountry.getmCovidCountry());
holder.worldDataCasesCount.setText(covidCountry.getmCovidCases());
holder.worldDataDeathCount.setText(covidCountry.getmCovidDeath());
Picasso.get().load(countryFlagUrl).fit().centerInside().into(holder.countryFlags);
}
@Override
public int getItemCount() {
return covidCountries.size();
}
public void filteredList(ArrayList<CovidCountry> filteredCountries) {
covidCountries = new ArrayList<>();
covidCountries.addAll(filteredCountries);
notifyDataSetChanged();
}

public static class WorldDataViewHolder extends RecyclerView.ViewHolder {
TextView worldDataTitle, worldDataDeathCount, worldDataCasesCount, worldNewCasesData;
ImageView countryFlags;
public WorldDataViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
worldDataTitle = itemView.findViewById(R.id.WorldDataName);
worldDataDeathCount = itemView.findViewById(R.id.WorldDataDeathCount);
worldDataCasesCount = itemView.findViewById(R.id.WorldDataCasesCount);
countryFlags = itemView.findViewById(R.id.covCountryFlags);
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 class MainActivity extends AppCompatActivity implements WorldDataAdapter.OnItemClickListener {
public static final String COUNTRY_NAME = "countryName";
private static final String TAG = MainActivity.class.getSimpleName();
TextView title, totalDeath, totalDeathNum, totalCases, totalCasesNum, totalRecovered, totalRecoveredData, activePatient, activePatientData;
ProgressBar progressBar;
EditText editText;
ArrayList<CovidCountry> covidCountries;
RecyclerView WorldRecycleView;
WorldDataAdapter worldDataAdapter;
RecyclerView.LayoutManager mlayoutManager;

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

title = findViewById(R.id.Title);
totalCases = findViewById(R.id.TotalCases);
totalCasesNum = findViewById(R.id.TotalCasesData);
totalDeath = findViewById(R.id.TotalDeath);
totalDeathNum = findViewById(R.id.TotalDeathData);
progressBar = findViewById(R.id.Progressloader);
totalRecovered = findViewById(R.id.TotalRecovered);
totalRecoveredData = findViewById(R.id.TotalRecoveredData);
activePatient = findViewById(R.id.TotalActive);
activePatientData = findViewById(R.id.TotalActiveData);
editText = findViewById(R.id.search_country);

getData();
detDataFromServer();
buildRecycleView();

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}
});
}
private void buildRecycleView() {
WorldRecycleView = findViewById(R.id.RecycleView);
WorldRecycleView.setHasFixedSize(true);
mlayoutManager = new LinearLayoutManager(this);
worldDataAdapter = new WorldDataAdapter(this,covidCountries);
WorldRecycleView.setLayoutManager(mlayoutManager);
WorldRecycleView.setAdapter(worldDataAdapter);
}
private void filter(String country) {
ArrayList<CovidCountry> filteredCountries = new ArrayList<>();
for(CovidCountry item: covidCountries){
if (item.getmCovidCountry().toLowerCase().contains(country.toLowerCase())) {
filteredCountries.add(item);
}
}
worldDataAdapter.filteredList(filteredCountries);
}
@Override
public void onItemClick(int position) {
Intent  detailIntent = new Intent(this, Deploy_Test.class);
Log.e(TAG, "onclivk: " + ajay );
CovidCountry covidCountry = filteredCountries.get(position);
//CovidCountry detailCountry = covidCountries.get(position);
//detailIntent.putExtra(COUNTRY_NAME,detailCountry.getmCovidCountry());
detailIntent.putExtra(COUNTRY_NAME,covidCountry.getmCovidCountry());


Log.e(TAG, "onItemClick: " + covidCountry );
startActivity(detailIntent);
}

如何获取过滤列表的新位置并将 OnclickListener 设置为它?

好吧,我想我知道你做错了什么。

因此,您正在发送仓位并提取活动中的数据,但您正在从错误的列表中提取。

MainActivity

//add this to the member fields
private ArrayList<CovidCountry> filteredCountries;
private final Context context;
private ArrayList<CovidCountry> covidCountries;
private OnItemClickListener covidListener;
.......

//the filter method becomes like this
private void filter(String country) {
filteredCountries = new ArrayList<>();
for(CovidCountry item: covidCountries){
if (item.getmCovidCountry().toLowerCase().contains(country.toLowerCase())) {
filteredCountries.add(item);
}
}
worldDataAdapter.filteredList(filteredCountries);
}

这是我认为你搞砸的地方,有你在问题中没有显示的接口方法,称为onItemClick(position)

@Override
public void onItemClick(int position){
//I am sure here is your mistake
//I guess you are using the old list to get the data at the position
//you must be getting data like this from the filteredCountries 
CovidCountry covidCountry = filteredCountries.get(position);
........
........

}

您没有使用相同的数组列表,而是创建不同的arryalist并显示。

最新更新