私は自分のアプリケーションで進歩しましたが、ボタンの(この例ではimageview)状態を取得するために何が実装される必要があるのか把握しようとしています。ブックマークされているすべての行(_id)のエントリを保持するsqliteテーブルのカスタムアダプタ。ブックマークされたアイテムのカスタムアダプタ経由でステートをSQLiteテーブルに保存
以下、setSelected()を使用してイメージビューを変更できるカスタムSimpleCursorAdapterをブックマークしてテーブルに格納するためのメカニズムを作成する必要があります。私も...それを実装する方法がわからない、私がやって中に思い描くものにはなく
public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
private DxDbAdapter mDbHelper;
public DxSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = super.getView(position, convertView, parent);
ImageView image = (ImageView) row.findViewById(R.id.fav);
//Query for _id of row item and see if there is an entry for it on the favourite table
//Then change ImageView to being selected image (yellow star)
//Else leave default image (greyed out)
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
ImageView fav = (ImageView) v.findViewById(R.id.fav);
fav.setImageResource(R.drawable.ic_fav);
long rowID = (Long) v.getTag();
Toast toast = Toast.makeText(context, "" + rowID, Toast.LENGTH_SHORT);
toast.show();
mDbHelper = new DxDbAdapter(context);
int result = mDbHelper.isFav(rowID);
if (result == 0) {
v.setSelected(true);
}
else {
v.setSelected(false);
}
}
});
return row;
}
}
を//コメントを入れている、私はお気に入りを追加できるように関数を作成しています、私のデータベース用のアダプタを持っていますテーブルへのエントリがありますが、私はカスタムカーソルアダプタの中でどのようにDBを呼び出すことができないのか、別の方法でそれを調べる必要があるか分かりません。次のように
public class DxDbAdapter {
public static final String DIAG_ID = "_id";
public static final String DIAG_CAT = "category";
public static final String DIAG_SUB = "subcategory";
public static final String DIAG = "diagnosis";
public static final String DIAG_CODE = "diagcode";
public static final String FAV_CODE = "edid";
private static final String DATABASE_NAME = "dx";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
protected static Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String s;
try {
Toast.makeText(mCtx, "1", 2000).show();
InputStream in = mCtx.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i=0; i<statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
} catch (Throwable t) {
Toast.makeText(mCtx, t.toString(), 50000).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS DiagLookups");
onCreate(db);
}
}
public DxDbAdapter(Context context) {
this.mCtx = context;
}
public DxDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDb.close();
}
public Cursor fetch(int level, String param) {
Cursor cursor = null;
switch (level) {
case 0:
cursor = mDb.rawQuery("Select distinct _id, diagnosis, diagcode, favourite From DiagLookup Where category = ? order by diagnosis asc", new String[]{""+param});
break;
case 1:
cursor = mDb.rawQuery("Select distinct _id, diagnosis, diagcode, favourite From DiagLookup Where subcategory = ? order by diagnosis asc", new String[]{""+param});
break;
}
return cursor;
}
public Cursor fetchFavs() {
Cursor cursor = mDb.rawQuery("Select distinct _id, diagnosis, diagcode, favourite From DiagLookup Where favourite = 1 order by diagnosis asc", null);
return cursor;
}
public int isFav(Long param) {
Cursor cursor = mDb.rawQuery("Select favourite From DiagLookup Where _id = ?", new String[]{""+param});
int result = cursor.getInt(cursor.getColumnIndex(FAV));
return result;
}
私のテーブルの構造は次のとおりです。
CREATE TABLE IF NOT EXISTS Lookup (
_id INTEGER PRIMARY KEY,
category VARCHAR(150),
subcategory VARCHAR(150),
diagnosis VARCHAR(150),
diagcode VARCHAR(150),
favourite INTEGER)
私はデシベルとオープン()、それを作成listactivityを持っています。そこからデータをカーソルにフェッチ()し、それをDxSimpleCursorAdapterに渡します。
ご指摘いただければ幸いです。
おかげ
()コードholder.image =(ImageViewの)findViewById(R.id.the_id_of_the_image)を、 android.view.Viewをインポートしても、findViewByIDはDxSimpleCursorAdapterのために未定義であると言われています。私は以下を使用しようとしました\t \t row.setTag(holder.image.findViewById(R.id.fav));どのエラーも作成されませんでしたが、アプリケーションをエミュレートするときに強制終了エラーが発生し、NullPointerExceptionが原因でした。 – DeucePie
@DeucePie申し訳ありません。膨張した行レイアウトでそのIDを検索する必要があります。私の小さな編集を参照してください。 – Luksprog
bindView()で私のアプローチと同じように、getView()の使用の違いは何ですか? – DeucePie