0

我需要对包含图像和两个字段的 ListView 的 ArrayAdapter 进行操作,但我不知道该怎么做。我可以试着做这个

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list, new ArrayList<ArrayList<string>>{ 
         tmp,         
                 wallResults.get("text"),        
                 }, new int[]{    
                 R.id.text1,      
                 R.id.text2});

但这不起作用

这是 list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:orientation="vertical"
  android:layout_height="wrap_content">

   <LinearLayout
  android:layout_width="265dip"
  android:orientation="vertical"
  android:layout_height="wrap_content">

  <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/text1"
  android:textSize="25dip"
  android:text="This is text1"/>

  <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/text2"
  android:text="This is text2"/>

  <ImageView android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/text2"
  android:text="This is text2"/>


  </LinearLayout>


</LinearLayout>
4

2 回答 2

0

数组适配器只允许一个使用 ArrayAdapter 的字段您可以使用安装的 SimpleAdapter 类。

ArrayList<String> listText1 = new ArrayList<String>();
    listText1.add("A");
    listText1.add("B");
    listText1.add("C");
ArrayList<String> listText2 = new ArrayList<String>();
    listText2.add("X");
    listText2.add("Y");
    listText2.add("Z");

List<Map<String, String>> alist = new ArrayList<Map<String, String>>();
Map<String, String> map;
int count = items.size();
for(int i = 0; i < count; i++) {
    map = new HashMap<String, String>();
    map.put("txt1", listText1.get(i));
    map.put("txt2", listText2.get(i));
    alist .add(map);
}

SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list, new String[] { "txt1", "txt2" }, new int[]{R.id.text1, R.id.text2});
于 2013-05-10T17:24:03.303 回答
0

使用这样的自定义适配器

public class TestListAdapter extends ArrayAdapter<Uri> {


    private LayoutInflater mInflater = null;
    private HttpImageManager mHttpImageManager = null;
    private ArrayList < SeedObject > seedData = null;


    public TestListAdapter(Activity context, ArrayList<SeedObject> seedList, List<Uri> uris) {
        super(context, 0, uris);

        mInflater = context.getLayoutInflater();
        this.seedData = seedList;

        mHttpImageManager = ((TestApplication) context.getApplication()).getHttpImageManager();
    }


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

        final ViewHolder holder;

        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.custom_row, null);

            holder.url = (TextView) convertView.findViewById(R.id.textSeed_seedsList);
            holder.city = (TextView) convertView.findViewById(R.id.textView_seed_city);
            holder.image = (ImageView) convertView.findViewById(R.id.imageSeed_seedsList);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Uri uri = getItem(position);
        ImageView imageView = holder.image;
        if (uri != null) {
            Bitmap bitmap = mHttpImageManager.loadImage(new HttpImageManager.LoadRequest(uri, imageView));
            if (bitmap != null) {
                // image download now compress and set on Image view
                bitmap = Bitmap.createScaledBitmap(bitmap, 60, 60, true);
                imageView.setImageBitmap(bitmap);
            } else if (position % 2 == 0) {
                imageView.setImageResource(R.drawable.greenseed_new);
            } else {
                imageView.setImageResource(R.drawable.grayseed_new);
            }



        }



        //set the seed name
        holder.url.setText(seedData.get(position).getTitle());
        String distance = seedData.get(position).getDistance();
        if (distance == null) {
            holder.city.setText(seedData.get(position).getAddress());
        }




        return convertView;
    }

    private static class ViewHolder {
        ImageView image;
        TextView city;
        TextView url;
    }
}
于 2013-05-10T17:17:43.240 回答