Joy
Joy

Reputation: 289

spinner selected item to open new array list using database android

I have display multiple row in the RecyclerView and top of the one filter select the filter open the Spinner and select the Spinner item to open only selected list how to show
i m new in android programming

My class

private List<People> peolesListAll = new ArrayList<>();
private RecyclerView recyclerView;
private AlertAllCustomeAdapter alertAllCustomAdapter;
private DataBaseHelper db;
Spinner spinner;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alertlist_all);

    BuildData();

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    spinnerData();

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    alertAllCustomAdapter = new AlertAllCustomeAdapter(this, peolesListAll);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(alertAllCustomAdapter);
    alertAllCustomAdapter.notifyItemRangeChanged(0, alertAllCustomAdapter.getItemCount());

    private void spinnerData() {
    db = new DataBaseHelper(getApplicationContext());
    List<String> lables = db.getAllLabels();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // lables.set(0, "ALL");
    spinner.setAdapter(dataAdapter);

}
private List<People> BuildData() {
    db = new DataBaseHelper(getApplicationContext());

    try {
        db.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    if (db.open()) {
        peolesListAll = db.getAllPeople();

    }
    return peolesListAll;
}

 @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

Upvotes: 0

Views: 667

Answers (1)

Fran&#231;ois Legrand
Fran&#231;ois Legrand

Reputation: 1233

In the onItemSelected method you can retrieve the selected filter with position parameter from your spinner data (here the List lables). Then, re-populate your recylcer adadpter and call adapter.notifyDataSetChanged() to update the view like this :

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String filter = (String) spinner.getAdapter().getItem(position);
    // update your peolesListAll according to the selected filter (with loop or anything else)
    alertAllCustomAdapter.setData(peolesListAll) // write setData(List<People> data)
    alertAllCustomAdapter.notifyDataSetChanged();
}

Upvotes: 1

Related Questions