如何获取字段值而不是列名?

发布于 2024-11-25 11:31:22 字数 1962 浏览 2 评论 0原文

我想通过这种方式用光标中的数据填充列表:

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

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

    myListAdapter.setViewBinder(VIEW_BINDER);

    companiesListView.setAdapter(myListAdapter);

我使用自定义 ViewBinder 来填充每行中的两个 TextViews 和一个 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 包含对 Cursor 的 moveToFirst 调用,但我仍然得到列名称:

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

I want to fill a list with data from a cursor this way:

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

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

    myListAdapter.setViewBinder(VIEW_BINDER);

    companiesListView.setAdapter(myListAdapter);

I use a custom ViewBinder to fill two TextViews and one ImageView in each row:

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;
    }
};

My problem is the cursor.getString() method call returns the column name not the value of the field and so I get hundreds of rows with column names.

Update:

My getAllCompanies includes a moveToFirst call on the Cursor but still I get column names:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-12-02 11:31:22

您是否尝试将光标移动到第一个索引 cursor.moveToFirst(); 然后执行 cursor.getString(columnIndex);

还有另一种方法,创建一个列表或数组来保存游标中的值,通过迭代游标访问其中的所有数据

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

,然后执行

iconName=list.get(columnindex);

此解决方案不是最好的,但无论如何都能达到目的。

Did you tried to move the cursor to the first index cursor.moveToFirst(); then do cursor.getString(columnIndex); ?

There is another way, create one List or array that will hold the values from the cursor, access all the data inside it by iterating through the cursor

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

then just do

iconName=list.get(columnindex);

This solution is not the best but how ever serves the purpose.

西瓜 2024-12-02 11:31:22

你尝试过吗?

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

它会从column_name中获取字符串

Did you try?

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

it will get you the string from the column_name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文