从数据库中选择id =来自spinner的值

问题描述 投票:0回答:2

我有以下问题:我有两个微调器,第一个有categorie1,第二个类别。

每个微调器都可以从表cat1和cat2中的数据库加载它的数据。但那不是我想要的。我希望第二个微调器加载数据,其中cat1_id等于当前所选spinner1的cat_id。

表格如下:

cat1:
| cat1_id | cat1_name |
=======================
| 1       | animal    |
| 2       | plant     |
| 3       | ...       |

cat2:
| cat2_id | cat1_id | cat2_name |
=================================
| 1       | 1       | fish      |
| 2       | 1       | mammal    |
| 3       | 1       | bird      |
| 4       | 2       | tree      |
| 5       | ...     | ...       |

如何进行此比较或如何知道在spinner1中选择了哪个项目。

我的代码是这样的:

DatabaseHandler.class:

    public List<String> getCat(){
    labels = new ArrayList<String>();
    db = this.getReadableDatabase();
    cursor = db.rawQuery(SELECT_QUERY_CATEGORIE, null);  // SELECT_QUERY_CATEGORIE = "SELECT  * FROM  cat1";

    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    return labels;
}

public List<String> getSubcat(){
    labels = new ArrayList<String>();
    db = this.getReadableDatabase();
    //cursor = db.rawQuery(SELECT_QUERY_SUBCATEGORIE, null); // SELECT_QUERY_SUBCATEGORIE = "SELECT  * FROM  cat2";

    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(2));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return labels;
}

MainActivity.class:

...
spCat.setOnItemSelectedListener(this);
spSubcat.setOnItemSelectedListener(this);
loadSpinnerData();

private void loadSpinnerData() {
    List<String> lbCategorie = db.getCat();
    ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lbCategorie);
    catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spCat.setAdapter(catAdapter);


    List<String> lbSubcategorie = db.getSubcat();
    ArrayAdapter<String> subcatAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lbSubcategorie);
    subcatAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spSubcat.setAdapter(subcatAdapter);
}

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

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // Spinner2.Visibility should be = GONE;
}

提前致谢。

编辑:好的。我正在使用以下代码加载我的数据库:

int position = MainActivity.spCat.getSelectedItemPosition()+1;
cursor = db.rawQuery("SELECT * FROM cat2 WHERE cat_id = ?" + position, null);

但是我的第二个Spinner是空的,无论我从第一个Spinner中选择什么。在调试时我注意到,加载数据库只是在开始时调用了一次。


编辑2:

删除“?”后立即可以使用在我的db.rawQuery中。但是,在第一个微调器中更改项目后,第二个微调器不是它的内容。在调用第一个微调器之后如何刷新第二个微调器?

谢谢。

android sql arraylist spinner
2个回答
0
投票

您可以执行查询:

Select * From cat2 where cat1_id==cat_id

0
投票

如果项目的顺序与表格中的类别相对应,您可以尝试使用catAdapter.selectedItemPosition,因为所选项目位置从0开始,也许您应该使用catAdapter.selectedItemPosition + 1,这样您就可以知道在spinner1上选择了什么值。你也可以用类似的东西

if (Integer.toString(catAdapter.selectedItem)=="1" 
{
  //put some code to tell number 1 is selected on the catAdapter
}
© www.soinside.com 2019 - 2024. All rights reserved.