这是我们从数据库获取信息的主要类。我们试图在我们创建的“条形码”数据库中获取“名称”列的值。现在,我们无法获得期望的值,但值为“a,b,c,d,...”(例如;当我们要求第一个返回“a”时,它返回“b”当我们问第二个在“名称”列)值与cursor.getString
方法android中的getString方法不起作用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch(SQLException sqle){
throw sqle;
}
Cursor cursor = myDbHelper.getAllAccounts();
String[] values = new String[6];
int i=0;
//take into the array from the database
while(cursor.moveToNext()){
values[i]= cursor.getString(cursor.getColumnIndex("name"));
i++;
}
cursor.close();
//Write to textview
TextView youtextview = (TextView) findViewById(R.id.textView1);
youtextview .setText(values[2]);
}
}
getString方法不起作用 - 是的。你的代码显然不会,但你没有清楚地描述问题所在。什么*确切*发生? – Simon 2013-04-29 18:45:14
我的意思是,而不是数据库中值[2]的值,我刚刚得到值“b”。当我将值[2]更改为值[1]时,我得到了“a”或值[3],我得到了“c”。在数据库中我没有任何数据,比如“a”,“b”或“c”。顺便说一下,我在SQLite数据库浏览器中创建了数据库。我的数据库看起来像这样: – user2255446 2013-04-29 19:11:14
它有一个“整数主键”的id列和“text”的名称列。 – user2255446 2013-04-29 19:12:37