我知道经常会问这个问题,但我仍然不知道它是如何工作的。
这是我的Activity.class
package de.retowaelchli.filterit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import de.retowaelchli.filterit.database.ADFilterDBAdapter;
public class ADeleteActivity extends Activity {
//Variablen deklarieren
private ADFilterDBAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autodelete);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent));
mDbHelper.open();
}
private static String[] mListContent={mDbHelper.getAllADFilter()};
/** Verweise auf die anderen Seiten **/
public void onClickADConfig(View view){
final Intent i = new Intent(this, ADFilterConfigActivity.class);
startActivity(i); }
}
这是我的Layout.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView android:id="@+id/AutoDeleteFilterListe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.97">
</ListView>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ADOverviewMainsite"
android:layout_weight="0.03">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ADFilterOverview"
android:onClick="onClickADConfig"
android:layout_weight="0.03">
<TextView
android:text="@string/newadfilter"
style="@style/NormalFont" />
</TableRow>
</TableLayout>
</LinearLayout>
继承我的DBAdapter:
package de.retowaelchli.filterit.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ADFilterDBAdapter {
public static final String ROW_ID = "_id";
public static final String NAME = "name";
public static final String KEYWORD = "keyword";
public static final String CACHE = "cache";
private static final String DATABASE_TABLE = "adfilter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx
* the Context within which to work
*/
public ADFilterDBAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the cars database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException
* if the database could be neither opened or created
*/
public ADFilterDBAdapter open() throws SQLException {
this.mDbHelper = new DatabaseHelper(this.mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
return this;
}
/**
* close return type: void
*/
public void close() {
this.mDbHelper.close();
}
/**
* Create a new car. If the car is successfully created return the new
* rowId for that car, otherwise return a -1 to indicate failure.
*
* @param name
* @param model
* @param year
* @return rowId or -1 if failed
*/
public long createADFilter(String name, String keyword, String cache){
ContentValues initialValues = new ContentValues();
initialValues.put(NAME, name);
initialValues.put(KEYWORD, keyword);
initialValues.put(CACHE, cache);
return this.mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Delete the car with the given rowId
*
* @param rowId
* @return true if deleted, false otherwise
*/
public boolean deleteADFilter(long rowId) {
return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
}
/**
* Return a Cursor over the list of all cars in the database
*
* @return Cursor over all cars
*/
public Cursor getAllADFilter() {
return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
NAME, KEYWORD, CACHE }, null, null, null, null, null);
}
/**
* Return a Cursor positioned at the car that matches the given rowId
* @param rowId
* @return Cursor positioned to matching car, if found
* @throws SQLException if car could not be found/retrieved
*/
public Cursor getADFilter(long rowId) throws SQLException {
Cursor mCursor =
this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME,
KEYWORD, CACHE}, ROW_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Update the car.
*
* @param rowId
* @param name
* @param keyword
* @param cache
* @return true if the note was successfully updated, false otherwise
*/
public boolean updateADFilter(long rowId, String name, String keyword,
String cache){
ContentValues args = new ContentValues();
args.put(NAME, name);
args.put(KEYWORD, keyword);
args.put(CACHE, cache);
return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
}
}
我尝试了我编写的代码,但是我遇到了错误,它只是不起作用。我应该如何改变它,或者我该怎么做?
答案 0 :(得分:1)
您正在返回游标值并将其保存在String array ...
中private static String[] mListContent={mDbHelper.getAllADFilter()};
尝试在列表适配器中添加游标值,如下所示:
ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
android.R.layout.two_line_list_item, // Specify the row template
// to use (here, two
// columns bound to the
// two retrieved cursor
// rows).
mCursor, // Pass in the cursor to bind to.
// Array of cursor columns to bind to.
new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME },
// Parallel array of which template objects to bind to those
// columns.
new int[] { android.R.id.text1, android.R.id.text2 });
// Bind to our new adapter.
setListAdapter(adapter);
你也可以参考这个文档: