Android的微调载荷多列(加盟)SQLite数据库到表 [英] Android spinner loads multicolumn (joined) Sqlite database into a table

查看:204
本文介绍了Android的微调载荷多列(加盟)SQLite数据库到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何创建一个从SQLite的下拉菜单加载一个微调。我有由一个喷丝和的表的用户界面。如果用户点击微调,表的内容将根据基于对微调所选择的ID数据库加载。如果没有选择的名称,它会加载所有内容表。但是我无法找到办法如何使基础上微调选择的ID /名称表重装。任何人都可以指导我?

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

表A :ID_Person |名称|年龄

表B :ID_Account | ID_Person |金额

微调显示人物的名字。同时,表将显示以下结构:

名称|年龄|金额

我的code的微调:

 公开名单<串GT; getAllDealers()
{
    清单<串GT; contentdealer =新的ArrayList<串GT;();
    //选择所有查询
    字符串selectQuery =SELECT * FROM+ Dealer_TABLE;    光标= db.rawQuery(selectQuery,NULL);
    //通过所有行循环,并增加列表
    如果(cursor.moveToFirst()){
        做{
            contentdealer.add(cursor.getString(1));
        }而(cursor.moveToNext());
    }
    //关闭连接
    返回contentdealer;
}

下面是如何我建立我的表循环:

 光标C = in.getViewInfo(); //在DB方法包括查询,我想表展    INT行= c.getCount();
    INT COLS = c.getColumnCount();
    c.moveToFirst();    //外循环
    的for(int i = 0; I<行;我++)
    {
        //循环基于.getCount所有行()
        //循环中的所有列
        对于(INT J = 0; J< COLS; J ++)
          {
          }
    }
    附寄();


解决方案

看到类似的,但这里不是完全一样的答案

<一个href=\"http://stackoverflow.com/a/11920785/1116836\">http://stackoverflow.com/a/11920785/1116836

我相信你想要的是

  spinner.setOnItemSelectedListener(新OnItemSelectedListener()
{
    @覆盖
    公共无效onItemSelected(适配器视图&LT;&GT; parentView,查看selectedItemView,INT位置,长的ID)
    {
        Toast.makeText(背景下,项目选中,加载一些SQL,Toast.LENGTH_LONG).show();
        //位置应与项目数组的索引列表时使用的哪个项目被选中,或在此你可以做
        字符串选择= spinner.getSelectedItem()的toString()。
        // 要么
        字符串anotherway = spinner.getItemAtPosition(位置)的ToString();
        如果(selected.equals(什么都可以选择是)){        }
    }    @覆盖
    公共无效onNothingSelected(适配器视图&LT;&GT; parentView)
    {
        Toast.makeText(背景下,没有什么选择,加载一些SQL,Toast.LENGTH_LONG).show();
    }
});

可能能够选择什么,你将不得不插入带无文本视图,并允许被选中。

onNothinSelected则多为列表时,进行修改,或当前选定的项目变成未选定,例如当它是在选择之间也可以调用此方法。


  

在选择从这个消失,要调用的回调方法
  视图。当触摸被激活时,选择可以消失,例如
  或者当适配器变空。


因此​​,基本上,当选择一个新的项目,调用加载某些SQL行的方法,清除表,然后显示新的数据。

编辑:发表评论

你所要做的是一个事件驱动功能。当有人选择在微调一个新的选择,这是一个事件。你听这个事件通过,正如我上面显示,实施 OnItemSelectedListener()

一,你实现这一点,你可以找到项目选择什么,只要它发生。一旦一个新的项目被选中,你需要确定哪些该项目意味着你应该做的。

一旦你想通了这一点,运行SQL语句查询数据库,加载数据,将其设置在适配器,然后用户会看到它。

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

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

一个快速谷歌搜索将您在任何时间运行。

I am learning how to create a spinner that loads dropdown from SQLite. I have an UI that consists of a spinner and a table. If user clicks spinner, the table's content will load according to the database based on the selected ID on spinner. If name not selected, it will load all contents in table.. However I can't find the way how to make the table reload based on the ID / name selected on spinner. Anyone can guide me?

The table itself is a joined table, which has following structure:

Table A : ID_Person | Name | Age

Table B : ID_Account | ID_Person | Amount

Spinner shows Person's name. Meanwhile the table will show the following structure:

Name | Age | Amount

My code for 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;
}

Here is how I build my Table for looping:

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

解决方案

See a similar but not exact same answer here

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

I believe what you want is

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

likely to be able to select nothing, you will have to insert a view with no text, and allow that to be selected.

onNothinSelected is more for when a list is modified, or the currently selected item becomes unselected, e.g. when it is in in between selections it may call this method.

Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty.

So basically, when a new item is selected, call a method that loads some sql rows, clear your table and then display the new data.

EDIT: for comment

What you are trying to do is an event drive function. When someone selects a new option in the spinner, it is an event. You listen for this event by, as I showed above, implementing the OnItemSelectedListener().

One you implement this, you can find out what item is selected, as soon as it happens. Once a new item is selected, you need to determine what that item means you should do.

Once you have figured that out, run your SQL statement and query your database, load the data, set it in the adapter and then the user will see it.

If you are using a ListView, which you should be, then you need to clear the adapter to the ListView, and then add the new items.

You need to research ArrayAdapter, BaseAdapter, ViewHolder pattern, and ListView's.

A quick google search will have you up and running in no time.

这篇关于Android的微调载荷多列(加盟)SQLite数据库到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆