创建SQLite数据库列表视图 [英] create listview from sqlite database

查看:199
本文介绍了创建SQLite数据库列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,在那里我可以创建和删除新记录。我想在另一个活动列表视图中显示该记录。

i have a database where i can create and delete new records. i want this records to be shown in a listview in another activity.

数据库:

public class DBHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "spinnerExample";

// Labels table name
private static final String TABLE_LABELS = "labels";

// Labels Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    // Category table create query
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
    db.execSQL(CREATE_CATEGORIES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

    // Create tables again
    onCreate(db);
}

/**
 * Inserting new lable into lables table
 * */
public void insertLabel(String label){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, label);

    // Inserting Row
    db.insert(TABLE_LABELS, null, values);
    db.close(); // Closing database connection
}

/**
* Removing label
* */
public void removeLabel(String label){
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, label);

// Removing Row
db.delete(TABLE_LABELS, KEY_NAME + "=?", new String[] {label});
db.close(); // Closing database connection
}

/**
 * Getting all labels
 * returns list of labels
 * */
public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}
}

列表视图的布局:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Modules"
    android:layout_margin="10dp" 
    android:textStyle="bold"
    android:textSize="30dp"
    android:layout_gravity="center"/>

<ListView
    android:id="@+id/lst_module"
    android:layout_width="match_parent"
    android:layout_height="330dp" 
    android:layout_marginRight="7dp"
    android:layout_marginLeft="7dp"
    android:layout_marginTop="7dp" >
</ListView>

<Button
    android:id="@+id/btn_edtmodule"
    android:layout_width="fill_parent"
    android:layout_height="64dp"
    android:text="Edit Modules" 
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"/>

 </LinearLayout>

活动类:

  public class Module extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.module);

    Button editModule = (Button) findViewById(R.id.btn_edtmodule);
    editModule.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent("sg.edu.tp.iit.mns.VotingAppMain"));           
        }
    });        
}

我想知道什么是code为活动课..有人可以帮助我。即时通讯非常新的这个Android的世界。

i would like to know what is the code for the activity class.. can someone please help me out. im very new to this android world.

推荐答案

有关显示列表视图中的所有项目首先创建自定义ListView和一个ArrayList中获取从数据库和存储数据

For displaying all your item in list view first create Custom listView and fetch data from database and store in a ArrayList

Class Data{
    String name,Address;
}

ArrayList<Data>list=new ArrayList<Data>();

Cursor userListCursor = dbAdapter.fetchAllRecord();
startManagingCursor(userListCursor);

while (!userListCursor.isAfterLast()) {
    Data record=new Data();
    record.name=userListCursor.getString(0);
    record.address=userListCursor.getString(1);
    list.add(record);
    userListCursor.moveToNext();
}

发此的ArrayList 你的类的扩展 BaseAdapter
并根据 getView()方法来设置这些值。

send This arraylist to your class that extends BaseAdapter class and under getView() method set these values.

这篇关于创建SQLite数据库列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆