如何获得字段的值,而不是列名? [英] How to get field value instead of column name?

查看:275
本文介绍了如何获得字段的值,而不是列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个光标这样的数据填充列表:

  myCursor = myDBA.getAllCompanies();
    startManagingCursor(myCursor);    myListAdapter =新SimpleCursorAdapter(这一点,R.layout.company_row,myCursor,FROM,TO);    myListAdapter.setViewBinder(VIEW_BINDER);    companiesListView.setAdapter(myListAdapter);

我使用自定义的 ViewBinder 来填补两个 TextViews 和一个的ImageView 每一行:

 私人最终ViewBinder VIEW_BINDER =新ViewBinder(){
    / **
     *通过绑定指定索引的定义光标列
     *指定视图。
     * /
    @覆盖
    公共布尔setViewValue(查看视图,光标光标,诠释参数:columnIndex){
        INT viewId = view.getId();
        开关(viewId){
        案例R.id.company_name:
        案例R.id.company_desc:
            Log.d(TAG,绑定的TextView);
            TextView的venueText =(TextView的)视图。
            venueText.setText(cursor.getString(参数:columnIndex));
            返回true;        案例R.id.company_icon:
            Log.d(TAG,绑定ImageView的);
            ImageView的venueIcon =(ImageView的)视图。
            串iconName;                iconName = cursor.getString(参数:columnIndex); //这里我得到的列名不字段值            资源资源= myContext.getResources();
            可绘制可绘制= resources.getDrawable(资源
                    .getIdentifier(my.package:绘/
                            + iconName,NULL,NULL));
            venueIcon.setImageDrawable(绘制);
            返回true;
        }
        返回false;
    }
};

我的问题是 cursor.getString()方法调用返回的列名不字段的值,所以我得到数百行与列名。

更新:

我的 getAllCompanie S包括 moveToFirst 呼吁光标但我仍得到列名:

 公共光标getAllCompanies(){
    光标S = myDB.query(表,钥匙,WHERE,NULL,NULL,NULL,NULL);
    s.moveToFirst();
    返回S;
}


解决方案

你有没有试过将光标移动到第一个指数 cursor.moveToFirst(); 然后执行 cursor.getString(参数:columnIndex);

有另一种方法,创建一个列表或数组将从光标持有的价值观,通过光标迭代访问它里面的所有数据

 而(cursor.isAfterLast()== FALSE)
{
  list.add(cursor.getString(thestringcollumnindex);
 cursor.moveToNext();
}

然后就去做

  iconName = list.get(参数:columnIndex);

这解决方案是不是最好的,但怎么过服务的目的。

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

解决方案

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.

这篇关于如何获得字段的值,而不是列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆