ListView中的一个TextView中的两个数组

时间:2012-11-01 21:46:49

标签: android listview textview

我有一个问题,我很乐意得到答案。

我有两个数组,让我们说:

String[] name = {"Name Nameson", "Second Name"};
String[] number = {"111 11 111", "222 22 222"};

我希望我的ListView在一个项目中有两个TextView。(我试图用图片来说明这一点)。

我设法获得f.ex.用简单的

将我的名字数组放入listview
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.single_name, name));

我的main.xml包含了我的ListView,我的布局文件夹中有single_name.xml和single_number.xml。

由于我是这个网站的新手,我不允许添加图片,我会在这里说明一下:


HEADER


(第一项)

姓名Nameson

111 11 111


(第二项)

第二个名字

222 22 222


这取决于联系人的数量。

非常感谢任何帮助:)

提前致谢!

3 个答案:

答案 0 :(得分:1)

你必须建立自己的适配器。

您可以修改这个以供您使用,它使用视图模式来提高性能。

public class WifiListAdapter extends BaseAdapter {

    private ArrayList<Wifi> entries;
    private Location location;
    private boolean withDistance;

    public static class ViewHolder{
        public TextView ssid;
        public TextView crypt;
        public TextView distance;

    }

    public WifiListAdapter(ArrayList<Wifi> entries, Location location) {
        this.entries = entries;
        this.location = location;
        withDistance = true;
    }

    public int getCount() {
        return entries.size();
    }

    public Wifi getItem(int position) {
        return entries.get(position);
    }


    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if(convertView == null){
            vi =  MyApplication.getLayoutInflater().inflate(R.layout.wifi_list_item, null);
            holder = new ViewHolder();
            holder.ssid = (TextView)vi.findViewById(R.id.wifi_list_ssid);
            holder.crypt = (TextView)vi.findViewById(R.id.wifi_list_crypt);
            holder.distance = (TextView)vi.findViewById(R.id.wifi_list_distance);

            vi.setTag(holder);


        }
        else{
            holder = (ViewHolder)vi.getTag();
        }
        Wifi wifi = entries.get(position);

        holder.ssid.setText(wifi.ssid);

        StringBuilder cryptString = new StringBuilder(20);

        if(wifi.wep == 1)
            cryptString.append("WEP ");
        if(wifi.wpa == 1)
            cryptString.append("WPA ");
        if(wifi.wpa2 == 1)
            cryptString.append("WPA2 ");
        if(wifi.wps == 1)
            cryptString.append("WPS ");
        if(wifi.ess == 1)
            cryptString.append("ESS");


        String distanceString;
        if(withDistance) {
            float[] results = new float[1];
            Location.distanceBetween(location.getLatitude(), location.getLongitude(), wifi.lat, wifi.lng, results);
            distanceString = MyApplication.formatDistance(results[0]);
        } else
            distanceString = "unknown";

        holder.distance.setText(distanceString);

        holder.crypt.setText(cryptString.toString().trim());
        //holder.distance.setText(results[0] + "");


        holder.ssid.setTag(position);
        holder.crypt.setTag(position);
        holder.distance.setTag(position);


        return vi;
    }

}

答案 1 :(得分:1)

您需要构建自定义适配器以及自定义视图以表示列表中的每一行。

以下是带有2个textview和imageview的listview的简单适配器示例。根据需要修改以包含标题并取出imageview

package com.aquarius.customlistviewproject;


public class CustomListViewAdapter extends BaseAdapter{


 private ArrayList<String> album_names;

private ArrayList<String> num_photos;

 public Activity context;

public LayoutInflater inflater;

 public CustomListViewAdapter(Activity context, ArrayList<String> album_names ,     ArrayList<String> num_photos){

super();
this.album_names = album_names;
this.num_photos = num_photos;
this.context = context;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public int getCount() {
    // TODO Auto-generated method stub
    return album_names.size();
}

public class ViewHolder{

    ImageView thumbnail;
    TextView  title;
    TextView photos;
}

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

    ViewHolder holder;

    if(convertView == null){
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.listview_row, null);

        holder.thumbnail = (ImageView)convertView.findViewById(R.id.imgViewLogo);
        holder.title = (TextView)convertView.findViewById(R.id.txtViewTitle);
        holder.photos = (TextView)convertView.findViewById(R.id.txtViewDescription);


        convertView.setTag(holder);
    }

    else
        holder = (ViewHolder)convertView.getTag();
        holder.thumbnail.setImageResource(R.drawable.imgview_drawable);

        holder.title.setText(album_names.get(position));
        holder.photos.setText(num_photos.get(position));
    return convertView;
}

}

答案 2 :(得分:0)

您需要创建

的自定义适配器
ArrayAdapter<HashMap<String,String>>