2012-04-25 14 views
1

私のCarProfileテーブルからレコードを選択するカーソルがあり、カスタムアダプタを使用して ブランド、モデル、およびラインスピンプレートをスピナー選択に表示します。私の問題は、最初の商品については「ALL」と言う選択をしたいから、選択すればすべての車の情報を表示できるということです。私のコードは次のとおりです。カスタムカーソルアダプタと空の選択を持つスピナー

  // spinner 1 
     mDbAdapter.open(); 
     Cursor cursorCP = mDbAdapter.fetchAllProfiles(); 
     startManagingCursor(cursorCP); 
     mDbAdapter.close(); 

     MyCustomAdapter ad = new MyCustomAdapter(this, cursorCP); 
     spinCP.setAdapter(ad); 

    public class MyCustomAdapter extends CursorAdapter { 

    public MyCustomAdapter(Context context, Cursor c) { 
     super(context, c); 

    } 
    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView nameTextView = (TextView) view 
       .findViewById(android.R.id.text1); 

     String brand = cursor.getString(cursor 
       .getColumnIndex(DefaxedDbAdapter.KEY_BRAND)); 
     String model = cursor.getString(cursor 
       .getColumnIndex(DefaxedDbAdapter.KEY_MODEL)); 
     String licence = cursor.getString(cursor 
       .getColumnIndex(DefaxedDbAdapter.KEY_LICENCEPLATE)); 
     nameTextView.setText(brand + "-" + model + "-" + licence); 
    } 
    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View view = View.inflate(context, 
       android.R.layout.simple_spinner_dropdown_item, null); 
     return view; 

    } 
} 

答えて

0

OK終わりに私の修正はIDを格納するハッシュマップの助けを借りて、アレイアダプタを使用することでした。ここで私は

 ArrayList<String> arr = new ArrayList<String>(); 
    arr.add(DefaxedDbAdapter.ALL_LABEL); 

    hm = new HashMap<String, Integer>(); 

    while (!cursorProfiles.isAfterLast()) { 
     int rowId = cursorProfiles.getInt(cursorProfiles 
       .getColumnIndex(DefaxedDbAdapter.KEY_ROWID)); 
     String brand = cursorProfiles.getString(cursorProfiles 
       .getColumnIndex(DefaxedDbAdapter.KEY_BRAND)); 
     String model = cursorProfiles.getString(cursorProfiles 
       .getColumnIndex(DefaxedDbAdapter.KEY_MODEL)); 
     String licencePlate = cursorProfiles.getString(cursorProfiles 
       .getColumnIndex(DefaxedDbAdapter.KEY_LICENCEPLATE)); 
     arr.add(brand + "-" + model + "-" + licencePlate); 
     hm.put(brand + "-" + model + "-" + licencePlate, rowId); 
     cursorProfiles.moveToNext(); 
    } 

    ArrayAdapter<String> adapterProfiles = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_dropdown_item, arr); 
    profilesSpinner.setAdapter(adapterProfiles); 

を使用して、スピナーから項目を選択した後、あなたがその

   if (profilesSpinner.getSelectedItemId() != 0) { 

       String tmp = profilesSpinner.getSelectedItem().toString(); 
       carId = hm.get(tmp); 

       if (alreadySet) { 
        SQLquery += " AND expenses.carId =" + carId; 
       } else { 
        SQLquery += " WHERE expenses.carId =" + carId; 
       } 
      } 
のようなIDを取得することができますコードがあります
関連する問題