Android:将SQL数据放入数组中

时间:2011-12-24 20:20:01

标签: android sql

我有两个方法,一个用于收集SQL数据,另一个用于将其放入listview中...似乎我无法在SQL部分中正确地形成数组,以便可以将其传递给listview。 / p>

SQL act:

public String[] getDataInArray() { // get data for list and return in array form
    // TODO Auto-generated method stub

    String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
    String[] return_colums = null;

    Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int rowcount = 0;

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
        rowcount = rowcount + 1;
    }

    return return_colums;
}

listview法案:

public ListView whiskeylist;
public String[] DataArryWhiskey;  

      @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Start db view of whiskey

    DBConfig whiskeyrows = new DBConfig(this);


    whiskeyrows.open();
    DataArryWhiskey = whiskeyrows.getDataInArray();
    whiskeyrows.close();

    Toast.makeText(MainScreen.this, result, Toast.LENGTH_LONG).show();

    whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
    whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));


    // End db view of whiskey

}// end onCreate

我一直在崩溃,有人可以帮忙见面吗? Thx提前

2 个答案:

答案 0 :(得分:1)

移动了返回列定义和声明。

答案 1 :(得分:0)

whiskeyrows.close();onCreate()移至onDestroy()。有关此link

的详细信息