2013-05-10 1 views
0

Мне нужно сделать ArrayAdapter для 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> 

ответ

0

массива адаптер позволяет только один поданный с использованием ArrayAdapter Вы можете использовать SimpleAdapter c - спросила девушка.

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