如何从sqlite数据库中获取数据并显示在listview中

 我来答
xiangjuan314
推荐于2018-05-09 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2849万
展开全部
在登录页面后,我想在listview中把Apple显示成A,Boy显示成B等等,直到F。但是在程序中当我完全登录后,只有登录表成功创建,主菜单还是没有创建。
我想在test database中创建主菜单,然后我想从主菜单表(mainmenu table)中获取数据再显示在listview中。
我使用了下面的代码:
if(username.length()>0&&password.length()>0)
{
SQLiteAdapter db=new SQLiteAdapter(Main.this);
db.openToWrite();
if(db.Login(username,password))
{
System.out.println("goutham");
Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);

startActivity(intent);
}

SQLiteAdapter.java
}

public Cursor queueAll() {
String[] columns = new String[] { KEY_ID, KEY_CONTENT };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
null, null, null, null);

return cursor;
}

private static class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

public long AddUser(String username, String password) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
return sqLiteDatabase.insert(DATABASE_TABLE, null, initialValues);

}

public boolean Login(String username, String password) {
// TODO Auto-generated method stub
Cursor mCursor = sqLiteDatabase.rawQuery("SELECT * FROM "
+ DATABASE_TABLE + " WHERE username=? AND password=?",
new String[] { username, password });
if (mCursor != null) {
if (mCursor.getCount() > 0) {
return true;
}
}
return false;
}

}

ExampleActivity.java
public class ExampleActivity extends Activity {
private SQLiteAdapter mySQLiteAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView) findViewById(R.id.contentlist);

/*
* Create/Open a SQLite database and fill with dummy content and close
* it
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
// mySQLiteAdapter.deleteAll();

mySQLiteAdapter.insert("A for Apply");
mySQLiteAdapter.insert("B for Boy");
mySQLiteAdapter.insert("C for Cat");
mySQLiteAdapter.insert("D for Dog");
mySQLiteAdapter.insert("E for Egg");
mySQLiteAdapter.insert("F for Fish");

mySQLiteAdapter.close();

/*
* Open the same SQLite database and read all it's content.
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();

Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);

String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
int[] to = new int[] { R.id.text };

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

mySQLiteAdapter.close();

}
}

运行程序后,登录表在数据库中创建了,但是主菜单表没有创建。运行程序后,显示一个错误:
sqlite returned code=1 no such a table in MY_TABLE

通过互联网整理获得以下解决方法:

=================1楼=====================
Database class:
public String getData1() throws SQLException{
// TODO Auto-generated method stub
String[] columns1 = new String[] { KEY_DATE };
Cursor c1 = ourDatabase.query(DATABASE_MARKSTABLE, columns1, null, null, null,
null, KEY_ENDINGTIME+" DESC", " 30");
String result1 = "";

int isName = c1.getColumnIndex(KEY_DATE);

for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {

result1 = result1 + c1.getString(isName)
+ " " + "\n";
}
c1.close();
return result1;
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
育知同创教育
2016-04-15 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
代码:
if(username.length()>0&&password.length()>0)
{
SQLiteAdapter db=new SQLiteAdapter(Main.this);
db.openToWrite();
if(db.Login(username,password))
{
System.out.println("goutham");
Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);

startActivity(intent);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式