2012-03-21 5 views
0

私は自分のアプリケーションで進歩しましたが、ボタンの(この例では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に渡します。

ご指摘いただければ幸いです。

おかげ

答えて

1

カーソルにアクセスすることができますので、あなたはbindView()newView()メソッドを実装する必要があります。 newView()では、レイアウトを膨らま:

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(context); // you should put this in the constructor so you don't do it every time the newView() method is called 
     View row = inflater.inflate(R.layout.row_layout, null); 
     // even implement the view holder pattern for a small performance boost 
     ViewHolder holder = new ViewHolder(); 
     holder.image = (ImageView) row.findViewById(R.id.the_id_of_the_image); 
     row.setTag(holder); 
     return row; 
} 

ViewHolderは、我々は全体のレイアウトに我々が行ビューにアクセスするたびに検索されませんので、ビューを保持するアダプタ内のクラスです:

class ViewHolder { 
    ImageView image; 
    // other views that you have in your row layout 
} 

そしてbindView()方法インプリメント:newView下

@Override 
public void bindView(View v, Context con, Cursor cursor) { 
     ViewHolder holder = (ViewHolder) v.getTag() //retrieve the holder with the row views 
     // set the current image status 
     int status = cursor.getInt(cursor.getColumnIndex(FAV_CODE)); // get the status(I don't know how you store the favorite status, I assumed is an int in the FAV_CODE column, 1 for favorite, 0 for unpicked yet) 
     if (status == 1) { 
      // the user set as favorite 
      holder.image.setImageResource(R.drawable.favorite); // set the favorite drawable 
     } else { 
     // this isn't a favorite so put the default image 
     holder.image.setImageResource(R.drawable.normal); // set the normal drawable 
     } 
     //get the row id from the cursor that we will pass in the onClick method as a tag for the image 
     long id = cursor.getLong(cursor.getColumnIndex(DIAG_ID)); 
     holder.image.setTag(new Long(id)); 
     // set the image listener 
     holder.image.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v){ 
      long rowID = (Long) v.getTag(); 
      //there is no need to find the view, and you don't modify the imageview image from here  
      // query the database to find the row with the _id equal to rowID 
      // find out what value we have in the column with the image status (either 1 or 0 meaning favorite or not favorite) 
      // update the row with the new status (if you have 1 in the database then update the status with 0 , if you have 0 in the database then update the value with 1) 
      //call notifyDatasetChanged on the adapter to let the adapter know about the update, I don't know if this call will work with SimpleCursorAdapter, you may have to set a new adapter on the ListView with a new cursor. 
     } 
     }); 

} 
+0

()コード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

+0

@DeucePie申し訳ありません。膨張した行レイアウトでそのIDを検索する必要があります。私の小さな編集を参照してください。 – Luksprog

+0

bindView()で私のアプローチと同じように、getView()の使用の違いは何ですか? – DeucePie

関連する問題