加行ArrayAdapter的Andr​​oid的ListVie

加行ArrayAdapter的Andr​​oid的ListVie

本文介绍了造型加行ArrayAdapter的Andr​​oid的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,我想与 ArrayAdapter 用于添加不同风格的行。该行将在不同的状态创造了我的申请,并根据不同的状态行应样式(如颜色和东西)。

I have a ListView that I want to use with an ArrayAdapter to add different styled rows. The rows are created on different states in my application, and depending on the different states the rows should be styled(like colors and stuff).

下面是一些伪code:

Here is some pseudo-code:

在创建:

mArrayAdapter = new ArrayAdapter(this, R.layout.message);
mView = (ListView) findViewById(R.id.in);
mView.setAdapter(mArrayAdapter);

在不同的状态,这是通过使用了MessageHandler另一个线程触发,将行添加到包含消息的列表:

On different states, which is triggered by another thread using a MessageHandler, add a row to the list containing a message:

mArrayAdapter.add("Message");

这工作得很好,消息在根据不同的状态列表雨后春笋般冒出来,但我希望有不同风格的行。如何做到这一点?是创建一个定制的解决方案 ArrayAdapter 与自定义add()方法?

This works fine, messages are popping up in the list depending on different states, but I want to have the rows styled differently. How to do this? Is the solution to create a custom ArrayAdapter with a custom Add() method?

推荐答案

您要做的是创建一个自定义的 ArrayAdapter 并重写 getView ()方法。在那里,你可以决定是否适用不同的风格的行或不行。例如:

What you have to do is creating a custom ArrayAdapter and override the getView() method. There you can decide whether apply a different style to the row or not. For instance:

class CustomArrayAdapter extends ArrayAdapter {
    CustomArrayAdapter() {
        super(YourActivity.this, R.layout.message);
    }

    public View getView(int position, View convertView,
                                            ViewGroup parent) {
        View row=convertView;

        if (row==null) {
            LayoutInflater inflater=getLayoutInflater();

            row=inflater.inflate(R.layout.message, parent, false);
        }

        // e.g. if you have a TextView called in your row with ID 'label'
        TextView label=(TextView)row.findViewById(R.id.label);
        label.setText(items[position]);

        // check the state of the row maybe using the variable 'position'
        if( I do not actually know whats your criteria to change style ){
            label.setTextColor(blablabla);
        }

        return(row);
    }
}

这篇关于造型加行ArrayAdapter的Andr​​oid的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:21