【问题标题】:Combining two ArrayAdapters组合两个 ArrayAdapter
【发布时间】:2012-11-28 17:18:33
【问题描述】:

我的应用程序使用来自 Eclipse 的标准 Master/Detail Flow Activity。我想知道是否可以更改某些“任务”的文本,每个任务都位于左侧。

masterData = new ArrayAdapter<MyTasks.taskItem>(getActivity(),
        R.layout.simple_list_item_activated_1,
        R.id.text1,
        MyTasks.ITEMS);
setListAdapter(masterData);

此代码将任务添加到主/从流中,它使用来自R.Layout 的常量。我无法更改代码,我尝试添加这行代码但没有任何反应:

android:textColor="#FF0000"

R.layout.simple_list_item_activated_1:

<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:textAppearance="?android:attr/textAppearanceListItemSmall" />

我也尝试过制作自己的 xml 文件(commenttextview.xml):

<?xml version="1.0" encoding="utf-8"?>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/commenttext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />

但没有什么真正有效。有人能帮我吗?

编辑:

我试过了,但现在我必须合并两个 ArrayList:

    nc_masterData = new ArrayAdapter<MyTasks.taskItem>(getActivity(),
            R.layout.nocommenttextview, R.id.nocommenttext);

    c_masterData = new ArrayAdapter<MyTasks.taskItem>(getActivity(),
            R.layout.commenttextview, R.id.commenttext);



    for (int i = 0; i < MyTasks.ITEMS.size(); i++) {
        if (MyTasks.ITEMS.get(i) != null) {
            if (MyTasks.customers[(Integer.parseInt(MyTasks.ITEMS.get(i).id))][10].equals("0")) {
                nc_masterData.add(MyTasks.ITEMS.get(i));
            }else{
                c_masterData.add(MyTasks.ITEMS.get(i));
            }
        }
    }


    setListAdapter(nc_masterData);

【问题讨论】:

    标签: android textview master-detail


    【解决方案1】:

    您可以尝试扩展 ArrayAdapter 类,并在其 getView 方法中更改 TextView 的颜色。

    编辑添加了一个示例,无法尝试,但我认为它应该可以工作。

    public class TestAdapter extends ArrayAdapter<MyTasks.taskItem> {
    
    private int resource;
    private int textViewResourceId;
    private Context context;
    private List<MyTasks.taskItem> objects;
    
    public TestAdapter(Context context, int resource, int textViewResourceId, List<MyTasks.taskItem> objects) 
    {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View view = vi.inflate(resource, null);
        TextView tv = (TextView) view.findViewById(textViewResourceId);
        tv.setText(objects.get(position).toString()); //Not sure about taskItem class toString
        tv.setTextColor(Color.parseColor("#FF0000"));
    
        return view;
    }
    

    }

    然后在你的主课中:

    masterData = new TestAdapter<MyTasks.taskItem>(getActivity(),
        R.layout.simple_list_item_activated_1,
        R.id.text1,
        MyTasks.ITEMS);
    
    setListAdapter(masterData);
    

    您应该查看 ViewHolder 模式以使适配器更有效。

    希望有帮助!

    【讨论】:

    • 编辑了我的答案以添加示例。
    • 谢谢!但我如何更改某些项目而不是所有项目的颜色(请参阅我编辑的帖子)
    • 可以根据getView方法中的位置来做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2014-12-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多