按数据库

时间:2018-06-07 08:46:59

标签: android sqlite listview simplecursoradapter

我正在使用listview开发应用程序,其中包含标签(图像),文本和收藏夹图标(图像)。我已经有一个列表视图,我直接设置图像名称,但我需要更改它 - 如果列表项未标记为收藏,则存储在db值0中;如果标记为标记,则存储为1,但显示空星或填充星。通过单击fav图标,它也应该更改db和icon中的值。所以,请帮助我在fav图标上的OnListItemClickListener(我已经在列表视图项上有它)并更改数据库中的最后一列。我该如何实现呢? 这里有我的清单项目

<ImageView
    android:id="@+id/list_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/list_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/list_star"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/list_label"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="lutti" />

<ImageView
    android:id="@+id/list_star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

这是我的ListViewActivity

    public class ListViewActivity extends AppCompatActivity  implements LoaderManager.LoaderCallbacks<Cursor> {


ListView lvData;
DB db;
SimpleCursorAdapter scAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    db = new DB(this);
    db.open();

    String[] from = new String[]{DB.COLUMN_IMG, DB.COLUMN_TXT, DB.COLUMN_IMG2};
    int[] to = new int[]{R.id.list_label, R.id.list_text, R.id.list_star};

    scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, null, from, to, 0);
    lvData = (ListView) findViewById(R.id.list);
    lvData.setAdapter(scAdapter);

    getSupportLoaderManager().initLoader(0, null, this);

    lvData.setOnItemClickListener(mOnListItemClickListener);
}

final Context context = this;

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
    return new MyCursorLoader(this, db);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    scAdapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
}

static class MyCursorLoader extends CursorLoader {

    DB db;

    public MyCursorLoader(Context context, DB db) {
        super(context);
        this.db = db;
    }

    @Override
    public Cursor loadInBackground() {
        Cursor cursor = db.getAllData();
        return cursor;
    }

}
// Create a message handling object as an anonymous class.
private AdapterView.OnItemClickListener mOnListItemClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {

        Cursor cursor = (Cursor) scAdapter.getItem(position);
Intent intent = new Intent(context, ViewPagerActivity.class);
        String pos = Long.toString(position);
        intent.putExtra("pos", pos);
        startActivity(intent);

    }
};
}

最后我的dB类

public class DB {

private static final String DB_NAME = "Poems_app_db"; ;
private static final int DB_VERSION = 1;
private static final String DB_TABLE = "poems";  ;

public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMG = "img";
public static final String COLUMN_TXT = "txt";
public static final String COLUMN_IMG2 = "img2";


private static final String DB_CREATE =
        "create table " + DB_TABLE + "(" +
                COLUMN_ID + " integer primary key autoincrement, " +
                COLUMN_IMG + " integer, " +
                COLUMN_TXT + " text, " +
                COLUMN_IMG2 + " integer " +
                ");";

private final Context mCtx;

private DBHelper mDBHelper;
private SQLiteDatabase mDB;

public DB(Context ctx) {
    mCtx = ctx;
}

public void open() {
    mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
    mDB = mDBHelper.getWritableDatabase();
}

public void close() {
    if (mDBHelper!=null) mDBHelper.close();
}

public Cursor getAllData() {
    return mDB.query(DB_TABLE, null, null, null, null, null, null);
}


/* public void addRec(String txt, int img) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TXT, txt);
    cv.put(COLUMN_IMG, img);
    mDB.insert(DB_TABLE, null, cv);
}*/

private class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context, String name, CursorFactory factory,
                    int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);

        ContentValues cv = new ContentValues();
        for (int i = 1; i < 5; i++) {
            cv.put(COLUMN_TXT, "sometext " + i);
            cv.put(COLUMN_IMG, R.drawable.ic_label_green);
            cv.put(COLUMN_IMG2, R.drawable.ic_star_filled);
            db.insert(DB_TABLE, null, cv);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
}

0 个答案:

没有答案