有没有办法用ArrayAdapter更新多个TextView的? [英] Is there a way to update multiple TextView with ArrayAdapter?

查看:368
本文介绍了有没有办法用ArrayAdapter更新多个TextView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图具有多个TextView的是这样的:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>
    <的TextView
        机器人:ID =@ + ID /名称
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =16dip
        机器人:文字颜色=#000000
        机器人:以下属性来=10dip
        机器人:TEXTSTYLE =黑体/>
    <的TextView
        机器人:ID =@ + ID /地址
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =16dip
        机器人:文字颜色=#000000
        机器人:paddingTop =15dip
        机器人:paddingBottom会=15dip
        机器人:以下属性来=10dip
        机器人:TEXTSTYLE =黑体/>
< / RelativeLayout的>
 

我POJO的的,有一个名称地址的清单,我想在列表视图中的每个项目被填充了这些值。

我的POJO是这样的:

 公共类Person {
  私人字符串名称;
  私人字符串地址;
  //吸气二传手
  公共字符串的toString(){返回名字;}
}
 

问题

当我设置列表适配器我的名单我怎么能同时设置名称和地址?

目前我正在做这里面只设置名称:

  setListAdapter(新ArrayAdapter<人>(MyActivity.this,R.layout.list_text,R.id.name,personList));
 

解决方案

您应该创建一个自定义的适配器扩展 ArrayAdapter 。这个事情你可以做一个 ArrayAdapter 是有限的。

事情是这样的:

 公共类PersonAdapter扩展ArrayAdapter<人> {
    私人最终上下文的背景下;
    私人最终的ArrayList<人>数据;
    私人最终诠释layoutResourceId;

    公共PersonAdapter(上下文的背景下,INT layoutResourceId,ArrayList的<人>数据){
        超级(上下文,layoutResourceId,数据);
        this.context =背景;
        this.data =数据;
        this.layoutResourceId = layoutResourceId;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        ViewHolder支架=无效;

        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新ViewHolder();
            holder.textView1 =(TextView中)row.findViewById(R.id.text1);
            holder.textView2 =(TextView中)row.findViewById(R.id.text2);
            ...
            ...
            holder.textView3 =(TextView中)row.findViewById(R.id.text3);

            row.setTag(保持器);
        }
        其他
        {
            支架=(ViewHolder)row.getTag();
        }

        人人= data.get(位置);

        holder.textView1.setText(person.getName());
        holder.textView2.setText(person.getAddress());
        ...
        ...
        holder.textView3.setText(person.getEtc());

        返回行;
    }

    静态类ViewHolder
    {
        TextView的textView1;
        TextView的textView2;
        ...
        ...
        TextView的textView3;
    }
}
 

其中, textView1 textView2 ... TextView的-N 都是你的文本视图。设为您的适配器如下:

  setListAdapter(新PersonAdapter(MyActivity.this,R.layout.list_text,personList));
 

注:我假设你的 personList 列表键入对象。如果它是一个阵列不要让我知道。

I have a list view that has multiple textview's like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>
</RelativeLayout>

I have a list of POJO's that has a name and address and I want each item in the list view to be populated with those values.

My POJO is like this:

public class Person {
  private String name;
  private String address;
  //getter setter
  public String toString() {return name;} 
}

Question

When I set the list adapter with my list how can I set both name and address?

Currently I'm doing this which is only setting the name:

setListAdapter(new ArrayAdapter<Person>(MyActivity.this, R.layout.list_text, R.id.name, personList));

解决方案

You should create a custom adapter that extends ArrayAdapter. The things you can do with an ArrayAdapter are limited.

Something like this:

public class PersonAdapter extends ArrayAdapter<Person> {
    private final Context context;
    private final ArrayList<Person> data;
    private final int layoutResourceId;

    public PersonAdapter(Context context, int layoutResourceId, ArrayList<Person> data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.data = data;
        this.layoutResourceId = layoutResourceId;
    }

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

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            holder.textView1 = (TextView)row.findViewById(R.id.text1);
            holder.textView2 = (TextView)row.findViewById(R.id.text2);
            ...
            ...
            holder.textView3 = (TextView)row.findViewById(R.id.text3);

            row.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)row.getTag();
        }

        Person person = data.get(position);

        holder.textView1.setText(person.getName());
        holder.textView2.setText(person.getAddress());
        ...
        ...
        holder.textView3.setText(person.getEtc());

        return row;
    }

    static class ViewHolder
    {
        TextView textView1;
        TextView textView2;
        ...
        ...
        TextView textView3;
    }
}

Where textView1, textView2 ... textView-n are all your text views. Set your adapter as follows:

setListAdapter(new PersonAdapter(MyActivity.this, R.layout.list_text, personList));

Note: I assume that your personList is a List type object. If it is an Array do let me know.

这篇关于有没有办法用ArrayAdapter更新多个TextView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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