2

次のように、カーソルからのデータでリストを埋めたいと思います。

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

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

    myListAdapter.setViewBinder(VIEW_BINDER);

    companiesListView.setAdapter(myListAdapter);

私はカスタムを使用して、各行にViewBinder2TextViewsつと1つを入力します。ImageView

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()メソッド呼び出しがフィールドの値ではなく列名を返すため、列名を持つ数百の行を取得することです。

アップデート:

getAllCompanieのsにはmoveToFirst呼び出しが含まれていますCursorが、それでも列名を取得します。

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

2 に答える 2

2

カーソルを最初のインデックスに移動しようとしましたcursor.moveToFirst();cursor.getString(columnIndex);

別の方法があります。カーソルからの値を保持する1つのリストまたは配列を作成し、カーソルを反復処理してその中のすべてのデータにアクセスします。

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

その後、

iconName=list.get(columnindex);

このソリューションは最善ではありませんが、目的を果たします。

于 2011-07-21T00:49:12.373 に答える
0

試しましたか?

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

column_name から文字列を取得します

于 2015-02-16T18:44:18.050 に答える