Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从sqlite数据库创建listview_Android_Database_Sqlite_Listview - Fatal编程技术网

Android 从sqlite数据库创建listview

Android 从sqlite数据库创建listview,android,database,sqlite,listview,Android,Database,Sqlite,Listview,我有一个可以创建和删除新记录的数据库。我希望此记录显示在另一个活动的列表视图中 数据库: public class DBHelper extends SQLiteOpenHelper { // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "spinnerExample"; // Label

我有一个可以创建和删除新记录的数据库。我希望此记录显示在另一个活动的列表视图中

数据库:

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"));           
        }
    });        
}

我想知道活动类的代码是什么。。谁能帮帮我吗。我对这个android世界非常陌生。

假设您只需要在ListView中显示数据库的内容,然后查看以下问题:


那里的代码正是这样做的,应该为您指明正确的方向。唯一的问题是,如果在单击后删除项目,您可能会遇到麻烦。

要在列表视图中显示所有项目,请先创建自定义列表视图,从数据库中获取数据并存储在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=新的ArrayList();
Cursor userListCursor=dbAdapter.fetchAllRecord();
startManagingCursor(userListCursor);
而(!userListCursor.isAfterLast()){
数据记录=新数据();
record.name=userListCursor.getString(0);
record.address=userListCursor.getString(1);
列表。添加(记录);
userListCursor.moveToNext();
}
将此
arraylist
发送到扩展
BaseAdapter
类的类 然后在
getView()
方法下设置这些值