簡體   English   中英

如何從 RecycleView Android 中獲取過濾數據的新 Position

[英]How to get the new Position of Filtered Data from RecycleView Android

我已經通過 Edittext 在 RecycleView 中實現了一個搜索過濾器。 我已將 onClickListener 設置為列表。 當我通過editext搜索時一切正常,數據顯示完美。 但是當我單擊過濾后的數據時。 它從舊列表中獲取 position,例如 0。

我的適配器 class

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

她是我的主要活動 class

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

如何獲取過濾列表的新 Position 並將 OnclickListener 設置為它?

您沒有使用相同的 arraylist,而是創建不同的 arryalist 和顯示。

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

因此,您發送 position 並提取活動中的數據,但您從錯誤的列表中提取。

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

........
........


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM