微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

登录页面后,我想在列表视图中显示A代表Apple,B代表Boy..ect.但是在我的应用程序中,我完全登录成功,仅登录表已完全创建成功,但未创建mainmenu.

      i try this code
      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 class sqliteAdapter {

public static final String MYDATABASE_NAME = "test";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String MYDATABASE_TABLE = "mainmenu";
private static final String DATABASE_TABLE = "login";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";

// create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE = "create table "
        + MYDATABASE_TABLE + " (" + KEY_ID
        + " integer primary key autoincrement, " + KEY_CONTENT
        + " text not null);";
private static final String DATABASE_CREATE = "create table login (_id integer primary key autoincrement, "
        + "username text not null, " + "password text not null);";

private sqliteHelper sqliteHelper;
private sqliteDatabase sqliteDatabase;

private Context context;

public sqliteAdapter(Context c) {
    context = c;
}

public sqliteAdapter openToRead() throws android.database.sqlException {
    sqliteHelper = new sqliteHelper(context, MYDATABASE_NAME, null,
            MYDATABASE_VERSION);
    sqliteDatabase = sqliteHelper.getReadableDatabase();
    return this;
}

public sqliteAdapter openToWrite() throws android.database.sqlException {
    sqliteHelper = new sqliteHelper(context, MYDATABASE_NAME, null,
            MYDATABASE_VERSION);
    sqliteDatabase = sqliteHelper.getWritableDatabase();
    return this;
}

public void close() {
    sqliteHelper.close();
}

public long insert(String content) {

    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_CONTENT, content);
    return sqliteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}

public int deleteall() {
    return sqliteDatabase.delete(MYDATABASE_TABLE, null, null);

}

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();

}

}

在运行程序登录表仅在数据库中创建但mainmenu表未在数据库中创建之后.运行程序后,它显示错误,例如sqlite返回代码= 1 MY_TABLE中没有这样的表

解决方法:

这是我用来获取所有值的代码.

/ *这是数据库类* /

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;

    }

并将其显示在列表视图中,请检查此链接

http://www.technotalkative.com/android-%E2%80%93-listview-%E2%80%93-2-custom-listview/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐