spinner选择项目使用数据库android打开新的数组列表

时间:2016-10-25 08:34:56

标签: java android database android-spinner

我在RecyclerView中显示多行并在一个过滤器的顶部选择过滤器打开Spinner并选择Spinner项目以仅打开所选列表如何显示
我是android编程新手

我的班级

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

}

1 个答案:

答案 0 :(得分:1)

position方法中,您可以使用微调器数据中的adapter.notifyDataSetChanged()参数检索所选过滤器(此处为列表标签)。然后,重新填充你的recylcer adadpter并调用@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(); } 来更新这样的视图:

int checkOperators(char str[], char operators[], int i){
    int counter = 0;
    while(operators[counter]!='\0')
    {
        if(str[i]==operators[counter]) return 1;
        counter++;
    }
  return 0;
}

int tokenise_ops(char str[], int start, char result[], char operators[]){

    int i = start;
    int j = start;

    while(str[i]==' ' && str[i]!='\0'){
        i++;
    }

    if(checkOperators(str,operators,i)==1)
    {
        result[j]= str[i];
        i++;
        return i;
    }

    while(str[i]!='\0')
    {
        result[j]= str[i];
        i++;
        j++;
        if(str[i]==' ' || checkOperators(str,operators,i)==1) return i;
    }

    return -1;
}

int main()
{
    char str[]="26.6 * 7.9 + 3";
    char result[256];
    char operators[]={'+','-','*','/','^','\0'};

    int start=0;

    start = tokenise_ops(str,start,result,operators);
    while ( start != -1 )
    {
        printf("%s\n", result);
        start = tokenise_ops(str, start, result,operators);
    }
    printf("%s\n", result);

    return 0;
}