Two arrays in one TextView in a ListView

烈酒焚心 提交于 2020-02-05 06:20:47

问题


I have a question that I would love to get an answer on.

I have two arrays, let's say:

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

I want my ListView to have two TextViews into one item.(I tried to illustrate this with a picture).

I manage to get f.ex. my name array into the listview with a simple

lv.setAdapter(new ArrayAdapter<String>(this, R.layout.single_name, name));

I have my main.xml with my ListView in it, single_name.xml and single_number.xml in my layout-folder.

Since I'm new to this site I'm not allowed to add a picture, I will try to illustrate it here:


HEADER


(item one)

Name Nameson

111 11 111


(item two)

Second Name

222 22 222


And this continues depending on quantity of contacts.

Any help would be greatly appreciated :)

Thanks in advance!


回答1:


You will need to build a custom Adapter as well a custom view to represent each row in your List.

Here's an example of a simple adapter for a listview with 2 textviews and an imageview. Modify as you see fit to include your header and take out the 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:


you have to build your own adapter.

You can modify this one for your use, it uses viewholder pattern for performance increase.

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;
    }

}



回答3:


You need to create a custom adapter of

ArrayAdapter<HashMap<String,String>>


来源:https://stackoverflow.com/questions/13186011/two-arrays-in-one-textview-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!