2011-07-20 23 views
2

私はカーソルからのデータで、このようにリストを埋めたい:列名の代わりにフィールド値を取得する方法?

  myCursor = myDBA.getAllCompanies(); 
    startManagingCursor(myCursor); 

    myListAdapter = new SimpleCursorAdapter(this, R.layout.company_row, myCursor, FROM, TO); 

    myListAdapter.setViewBinder(VIEW_BINDER); 

    companiesListView.setAdapter(myListAdapter); 

私は、各行に2 TextViewsと1 ImageViewを埋めるために、カスタムViewBinderを使用します。

private final ViewBinder VIEW_BINDER = new ViewBinder() { 
    /** 
    * Binds the Cursor column defined by the specified index to the 
    * specified view. 
    */ 
    @Override 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
     int viewId = view.getId(); 
     switch (viewId) { 
     case R.id.company_name: 
     case R.id.company_desc: 
      Log.d(TAG, "Binding TextView"); 
      TextView venueText = (TextView) view; 
      venueText.setText(cursor.getString(columnIndex)); 
      return true; 

     case R.id.company_icon: 
      Log.d(TAG, "Binding ImageView"); 
      ImageView venueIcon = (ImageView) view; 
      String iconName; 

       iconName = cursor.getString(columnIndex); //Here I get the column name not the field value 

      Resources resources = myContext.getResources(); 
      Drawable drawable = resources.getDrawable(resources 
        .getIdentifier("my.package:drawable/" 
          + iconName, null, null)); 
      venueIcon.setImageDrawable(drawable); 
      return true; 
     } 
     return false; 
    } 
}; 

私の問題がありますcursor.getString()メソッド呼び出しは、フィールドの値ではなく列名を返します。したがって、列名を持つ何百もの行が得られます。

更新:

マイgetAllCompanieCursormoveToFirstコールが含まれますが、まだ、私は列の名前を取得:

public Cursor getAllCompanies(){ 
    Cursor s = myDB.query(TABLE, KEYS, WHERE, null, null, null, null); 
    s.moveToFirst(); 
    return s; 
} 

答えて

2

を、あなたはその後、cursor.getString(columnIndex);を行う最初のインデックスcursor.moveToFirst();にカーソルを移動しようとしたしましたか?もう一つの方法は、カーソルから値を保持する1つのリストや配列を作成し、

あり、カーソルを使用して

while(cursor.isAfterLast()==false) 
{ 
    list.add(cursor.getString(thestringcollumnindex); 
cursor.moveToNext(); 
} 

を繰り返すことによって、それ内のすべてのデータにアクセスそしてちょうど

iconName=list.get(columnindex); 
を行います

このソリューションは最高ではありませんが、その目的にはどのように役立ちます。

+0

からあなたの文字列を取得します: 'myDBA.getAllCompanies(中' moveToFirst')を呼び出し、 '、あなたは問題ないはずです。 – Femi

+0

moveToFirstをどこから呼び出すのかは重要ではありません。 –

+0

それは正しいですが、それは 'Cursor'を返す関数に埋め込むのが最も簡単です。 'Cursor'を返すと、準備が整いました。 'moveToFirst'が失敗した場合はnullを返し、運動全体をスキップします。 – Femi

0

試しましたか?

cursor.getString(cursor.getColumnIndex("column_name")); 

それは、私は2番目、これはよcolumn_nameに

関連する問題