【问题标题】:Android spinner loads multicolumn (joined) Sqlite database into a tableAndroid spinner 将多列(连接)Sqlite 数据库加载到表中
【发布时间】:2014-10-21 22:37:05
【问题描述】:

我正在学习如何创建一个从 SQLite 加载下拉列表的微调器。我有一个由微调器和表格组成的 UI。如果用户单击微调器,则表的内容将根据基于微调器上所选 ID 的数据库加载。如果未选择名称,它将加载表格中的所有内容。但是我找不到如何根据微调器上选择的 ID / 名称重新加载表格的方法。谁能指导我?

表本身是一个连接表,其结构如下:

表 A:ID_Person |姓名 |年龄

表 B:ID_Account | ID_Person |金额

Spinner 显示人的名字。同时表格将显示以下结构:

姓名 |年龄 |金额

我的微调器代码:

public List<String> getAllDealers()
{
    List<String> contentdealer = new ArrayList<String>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " +Dealer_TABLE;

    cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            contentdealer.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }
    // closing connection
    return contentdealer;
}

这是我构建循环的表格的方法:

    Cursor c = in.getViewInfo(); //method in db consists of query that i want table show

    int rows = c.getCount();
    int cols = c.getColumnCount();
    c.moveToFirst();

    // outer for loop
    for (int i = 0; i < rows; i++) 
    {
        //looping all rows based .getCount()
        //looping all columns
        for (int j = 0; j < cols; j++) 
          {
          }
    }
    in.close();

【问题讨论】:

    标签: android database sqlite android-spinner android-tablelayout


    【解决方案1】:

    在此处查看类似但不完全相同的答案

    https://stackoverflow.com/a/11920785/1116836

    我相信你想要的是

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
            Toast.makeText(context, "item selected, load some sql", Toast.LENGTH_LONG).show();
            // position should match the index of the array in the items list you used for which item is selected, or here you could do
            String selected = spinner.getSelectedItem().toString();
            // or
            String anotherway = spinner.getItemAtPosition(position).toString();
            if (selected.equals("what ever the option was")) {
    
            }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parentView) 
        {
            Toast.makeText(context, "nothing selected, load some sql", Toast.LENGTH_LONG).show();
        }
    });
    

    可能无法选择任何内容,您必须插入一个没有文本的视图,并允许它被选中。

    onNothinSelected 更多用于当列表被修改,或者当前选中的项目变为未选中时,例如当它处于选择之间时,它可能会调用此方法。

    选择消失时调用的回调方法 看法。例如,当触摸被激活时,选择可能会消失 或者当适配器变空时。

    所以基本上,当一个新项目被选中时,调用一个加载一些 sql 行的方法,清除你的表,然后显示新数据。

    编辑:供评论

    您正在尝试做的是事件驱动功能。当有人在微调器中选择一个新选项时,它就是一个事件。如上所示,您可以通过实现 OnItemSelectedListener() 来监听此事件。

    一旦你实现了这一点,你就可以在它发生时找出选择了什么项目。选择新项目后,您需要确定该项目的含义。

    一旦你弄清楚了,运行你的 SQL 语句并查询你的数据库,加载数据,在适配器中设置它,然后用户就会看到它。

    如果你使用的是ListView,你应该是这样,那么你需要清除到ListView的适配器,然后添加新的项目。

    您需要研究 ArrayAdapter、BaseAdapter、ViewHolder 模式和 ListView 的。

    快速的 google 搜索将使您立即启动并运行。

    【讨论】:

    • 感谢您的来信。我应该在表格代码中做些什么以使其基于微调器 btw 重新加载?
    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多