gpt4 book ai didi

Android:将SQL数据放入数组

转载 作者:行者123 更新时间:2023-11-30 04:25:27 24 4
gpt4 key购买 nike

我有两种方法,一种用于收集 SQL 数据,一种用于将其放入 ListView 中。似乎我无法在 SQL 部分中正确形成数组,以便将其传递给 ListView 。

SQL 行为:

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

我老是崩溃,谁能帮帮我?提前致谢

最佳答案

移动返回列定义和声明。

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


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;
String[] return_colums = new String[c.getCount()];
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
rowcount = rowcount + 1;
}

return return_colums;
}

关于Android:将SQL数据放入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8626676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com