我在Android应用程序开发方面相当新,我试图从SQLite数据库中获取行以显示为Listactivity,但这不起作用,程序意外终止。这是代码:
package ankur.test.app;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class testMenu extends ListActivity{
HotOrNot Infor = new HotOrNot(this);
{
Infor.open();
}
String[] result = Infor.getList();
{
Infor.close();
}
@Override
protected void onCreate(Bundle ankurtestlist) {
// TODO Auto-generated method stub
super.onCreate(ankurtestlist);
setListAdapter(new ArrayAdapter<String>(testMenu.this, android.R.layout.simple_list_item_2, result));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Object cheese = result[position];
/* try{
Class ourClass = Class.forName("ankur.test.app."+ cheese);
Intent ourIntent = new Intent(testMenu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}*/
}
}
这是另一个类中的方法:
public String[] getList() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
ArrayList name = new ArrayList();
int i = 0;
int iName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
name.add(c.getString(iName));
i++;
}
String[] result = (String[]) name.toArray();
return result;
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
似乎您遇到了上下文问题,只需将新Infor = HotOrNot(this);
初始化为onCreate()
并编辑代码,
String[] result;
HotOrNot Infor;
@Override
protected void onCreate(Bundle ankurtestlist) {
super.onCreate(ankurtestlist);\
Infor = new HotOrNot(this);
Infor.open();
result = Infor.getList();
Infor.close();
setListAdapter(new ArrayAdapter<String>(testMenu.this, android.R.layout.simple_list_item_2, result));
}