简体   繁体   English

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

[英]how to fetch the data from sqlite database and show in listview in android

after login page I want show the A for Apple,B for Boy....ect upto F..in listview. 在登录页面之后,我想要显示A代表Apple,B代表男孩......等待F..in listview。 But in my application i login success fully only login table is created success fully but mainmenu is not created.. i want create mainmenu table in test database after that i want fetch the data from mainmenu table to listview in android 但在我的应用程序中我完全登录成功只有登录表完全成功创建但mainmenu没有创建..我想在测试数据库中创建mainmenu表之后,我想从mainmenu表获取数据到androidview中的listview

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

}

} }

after running program login table is only created in the database but mainmenu table is not created in the database.after run program it show an error like sqlite returned code=1 no such a table in MY_TABLE 运行程序后,登录表只在数据库中创建,但是在数据库中没有创建mainmenu表。在运行程序之后它显示一个错误,如sqlite返回的代码= 1在MY_TABLE中没有这样的表

this is the code which i am using to fetch all the values. 这是我用来获取所有值的代码。

/ * this is 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;

    }

and inorder to display it in the list view please check this link 并且为了在列表视图中显示它,请检查此链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android ListView从sqlite数据库获取数据 - Android listview fetch the data from sqlite database 如何在 Android ListView 中显示来自 SQLite 数据库的特定数据 - How To Show specific Data From SQLite Database In Android ListView 如何在 Android 的 ListView 中显示来自 SQLite 数据库的多个数据 - How to show multiple data from SQLite database inside ListView in Android Android-从SQLite获取数据并在ListView中显示 - Android - Fetch data from SQLite and display in ListView 如何从sqlite数据库中获取数据并通过在android中使用id在表单视图中显示它 - How to fetch data from sqlite database and show it in a form view by using id in android 如何从中心数据库获取数据并存储在android sqlite数据库中? - How to fetch data from center database and store in android sqlite database? 如何从android中的Sqlite数据库获取数据并在列表视图中打印 - how to fetch data and print in List view from Sqlite database in android 如何从mysql中获取数据并存储在android中的Sqlite数据库中 - How to fetch data from mysql and store in Sqlite database in android 如何在Android中的SQLite数据库中以ListView显示数据 - How to display data in listview from sqlite database in android 从android数据库sqlite获取特定数据 - Fetch specific data from android database sqlite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM