Android的ListFragment单独更改行背景颜色

Android的ListFragment单独更改行背景颜色

本文介绍了Android的ListFragment单独更改行背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListFragment,我想一定行一定的色差。我基本上遵循这样的:Creating一个ListView和设置每一行视图的背景颜色

I have a ListFragment where I want certain rows to be a certain color. I basically followed this: Creating a ListView and setting the background color of a view in each row

然而,getView不会被调用。有谁知道为什么吗?

However, getView is never called. Does anyone know why?

    public class TrackerFragment extends ListFragment
{
    private String[] list;
    @Override
    public void onActivityCreated(Bundle myBundle)
    {
        super.onActivityCreated(myBundle);

        list = null;
        ListView lv = getListView();
        setListAdapter(null);
        setEmptyText("Touch a connection to view tracker information.");
        lv.setTextFilterEnabled(true);
    }

    public void updateList(String[] list)
    {
        this.list = list;
        setListAdapter(new ColoredArrayAdapter(getActivity(),R.layout.list_item,list));
    }
}

list_item.xml

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="7dp"
    android:textSize="14sp"
    android:id="@+id/line">
</TextView>

我从我的活动更新列表如下:

I am updating the list like this from my activity:

TrackerFragment tf = (TrackerFragment) fragmentManager.findFragmentById(R.id.tracker1);
tf.updateList(result);

我ColoredArrayAdapter

My ColoredArrayAdapter

public class ColoredArrayAdapter extends ArrayAdapter{

    private String[] list;

    public ColoredArrayAdapter(Context context, int textViewResourceId,
            Object[] objects) {
        super(context, textViewResourceId, objects);
        list = new String[objects.length];
        for (int i = 0; i < list.length; i++)
            list[i] = (String) objects[i];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
{
    View vi = convertView;
    ViewHolder holder;

    if (convertView == null)
    {
        vi = LayoutInflater.from(getContext()).inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.line = (TextView) vi.findViewById(R.id.line);
        vi.setTag(holder);
    }
    else
        holder = (ViewHolder) vi.getTag();

    for (int i = 0; i < list.length; i++)
    {
        if (list[i].contains("OUT OF LOCK"))
        {
            System.out.println("OUT OF LOCK");
            holder.line.setText(list[i]);
            //holder.line.setTextColor(R.color.white);
            holder.line.setBackgroundResource(R.color.red);
        }
        else if(list[i].contains("IN LOCK"))
        {
            System.out.println("In LOCK");
            holder.line.setText(list[i]);
            //holder.line.setTextColor(R.color.white);
            holder.line.setBackgroundResource(R.color.green);
        }
        else
            holder.line.setText(list[i]);
    }

    return vi;
}
}

我是什么做错了吗?
编辑:添加list_item.xml,其中线被发现。
EDIT2:添加扩展阵列适配器

What am I doing wrong? added list_item.xml, where line is found. added extended array adapter

现在我的问题是这样的,每一行要么是全绿色或红色,当我只想某些个别行是红色或绿色。此外,没有任何的文字是自我表现。

Now my problem is that, every row is either all green or red, when I just want certain individual rows to be either red or green. Also, none of the text is showing up.

推荐答案

您当前的 getView 的实施应移入 ListAdapter 执行,而不是你的 TrackerFragment 类。由于您使用 ArrayAdapter ,你也可以继承这一点,把code在那里。 ArrayAdapter 已经实现了 getView ,但你会重写它提供您专业的行为。

Your current getView implementation should be moved into a ListAdapter implementation instead of your TrackerFragment class. Since you're using ArrayAdapter, you can subclass that and put the code in there. ArrayAdapter already implements getView, but you'll override it to provide your specialized behavior.

你得到一个NullPointerException异常的原因是因为你调用 getView ,并通过在列表视图,它没有一个标签与之关联的 - 所以 =持有人(ViewHolder)vi.getTag(); 受让人。这就是说,你不应该直接调用 getView 。该系统将调用你时,它需要显示列表项。当系统调用 getView 方法,它最初传入有意见创建,并且每一个电话,其中 convertView 不是是通过该方法创建的视图。

The reason you're getting a NullPointerException is because you're calling getView and passing in the list view, which does not have a tag associated with it -- so holder = (ViewHolder) vi.getTag(); assigns null to holder. That said, you shouldn't be calling getView directly. The system will call that for you whenever it needs to display a list item. When the system calls the getView method, it initially passes in null to have the views created, and every call where convertView is not null is a view created by that method.

这篇关于Android的ListFragment单独更改行背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:17