【问题标题】:android listview custom row layout manipulate (with Loaders)android listview 自定义行布局操作(带加载器)
【发布时间】:2013-12-22 10:21:43
【问题描述】:

我有一个 ListView,其中包含自定义 row_layout.xml,其中包含 3 个 TextView。

要在列表视图中显示数据,我使用 LoaderManager

我想根据其中一项的值将所有 3 个 TextViews 背景颜色更改为红色/绿色/橙色。

为此,我在我的适配器 (SimpleCursorAdapter) 中覆盖了 setViewValue

dataAdapter.setViewBinder(new ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor, int coloumIndex)
            {

                if (coloumIndex == cursor.getColumnIndex(SQLdataHelper.INVITE_STATUS))
                {

                    TextView tv = (TextView) view.findViewById(R.id.invite_Status);
                    InviteStatus status = InviteStatus.valueOf(cursor.getString(cursor.getColumnIndex(SQLdataHelper.INVITE_STATUS)));
                    switch(status)
                    {
                        case atProgress:
                        {
                            tv.setTextColor(Color.parseColor("#ffa500"));
                            break;
                        }

                        case Completed:
                        {

                            //TextView tv2 = (TextView) view.findViewById(R.id.Invite_RequestedDate);
                            //TextView tv3 = (TextView) view.findViewById(R.id.invite_Name);
                            tv.setTextColor(Color.GREEN);
                            //tv2.setTextColor(Color.GREEN);
                            //tv3.setTextColor(Color.GREEN);
                            break;
                        }

                        case Received:
                        {

                            tv.setTextColor(Color.RED);
                            break;
                        }

                    }
                    return true;
                }


                return false;
            }
        });

此代码正在运行。但它只会更改其中一项..

在 switch 案例中,我尝试获取其他 2 个项目的 TextView,但我得到 NullPointerException

如何为列表中的其他 2 个项目着色?

【问题讨论】:

    标签: android listview android-loadermanager


    【解决方案1】:

    实际上,您误解了 setViewValue() 的参数中写入的“视图”。 我认为,您认为此视图代表整个 xml 文件,但事实并非如此。

    例如,假设我们有一个 xml 文件,其中 LinearLayout 作为根视图,两个文本视图作为其子视图。然后将分别为每个 textview 调用方法 setViewValue()。

    谁来决定在哪些单独的视图上调用这个方法?好吧,SimpleCursorAdapter 初始化中提到的所有视图,即this constructor的“int[] to”

    阅读 docs 的 setViewValue() 方法。它说,此方法将指定索引定义的“光标列”绑定到“指定视图”。这个columnIndex是这个游标中与视图相关的列的索引。

    在你的情况下,可以这样做:

    dataAdapter.setViewBinder(new ViewBinder() {
    
            @Override
            public boolean setViewValue(View view, Cursor cursor, int coloumIndex)
            {
                // In you case.. this view itself is a textview so you can use it directly.
                boolean output = false;
                switch(view.getId()){
                    case R.id.invite_Status:{
                        InviteStatus status = InviteStatus.valueOf(cursor.getString(cursor.getColumnIndex(SQLdataHelper.INVITE_STATUS)));
                        // change
                        switch(status){
                                //changes to invite_status textview should be done here.
                                case atProgress:{
                                    view.setTextColor(Color.parseColor("#ffa500"));
                                    break;
                                }
                                // and so on
                        }
                        output = true;
                        break;
                    }
                    case R.id.Invite_RequestedDate:{
                        InviteStatus status = InviteStatus.valueOf(cursor.getString(cursor.getColumnIndex(SQLdataHelper.INVITE_STATUS)));
                        // change
                        switch(status){
                                //changes to invite_RequestedDate textview should be done here.
                        }
                        output = true;
                        break;
                    }
                    case R.id.invite_Name:{
                        // similar as above.
                    }
                }
                return output;
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多