ssummeraw 2013-05-03 06:11 采纳率: 10%
浏览 14803
已采纳

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

在登录页面后,我想在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条回答 默认 最新

  • Curie-87 2013-05-06 02:26
    关注

    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;
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联