2011-03-08 2 views
0

각 행마다 다른 이미지와 텍스트가 포함 된 목록보기를 만들고 싶습니다. 텍스트 및 이미지 용 배열을 만들었지 만, 더 이상 할 방법이 없습니까?diff를 사용하여 목록보기를 만드는 방법. 이미지와 안드로이드의 텍스트?

+0

이 링크는 도움이 될 수 있습니다. http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/ – Mudassir

+0

새로운 질문을하기 전에이 사이트에서 검색 기능을 사용해보십시오. 여기에 제공된 모든 소스 코드와 함께 멋진 답변을 찾을 수 있습니다 : [Listview with images] (http://stackoverflow.com/questions/541966/android-how-do-i-la-y-load-of- images-in-listview/3068012 # 3068012) – Adinia

답변

0
ListView list=(ListView)findViewById(android.R.id.list); 
    list.setAdapter(new EfficientAdapter(this)); 

    String[] alltext={..............}; 
    String[] alImages={..........}; 

private static class EfficientAdapter extends BaseAdapter 
{ 
    private LayoutInflater mInflater; 

    public EfficientAdapter(Context context) 
    { 
    mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() 
    { 
    return alltext.length; 
    } 

    public Object getItem(int position) 
    { 
    return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    ViewHolder holder; 
    if (convertView == null) 
    { 
    convertView = mInflater.inflate(R.layout.images, null); 
    holder = new ViewHolder(); 

    holder.title = (TextView) convertView.findViewById(R.id.title); 
    holder.image = (ImageView) convertView.findViewById(R.id.image); 

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

    holder.title.setText(alltext[position]);  
    holder.image.setImageBitmap(loadImageFromUrl(allImges[position])); 
    return convertView; 
    } 

    static class ViewHolder 
    { 
    TextView title; 
    ImageView image; 


    } 
    } 

당신은 이미지 URL은 다음이 하나

holder.image.setImageResource(allImges[position]); 

loadImageFromUrl 방법은

public static Bitmap loadImageFromUrl(String url) { 
    InputStream inputStream;Bitmap b,result; 

    try { 
      if(url.contains(" ")){ 
       url=url.replace(" ", "%20"); 
        } 


      inputStream = (InputStream) new URL(url).getContent(); 
      BitmapFactory.Options bpo= new BitmapFactory.Options(); 
      bpo.inJustDecodeBounds = true; 
      bpo.inJustDecodeBounds = false; 

      if(bpo.outWidth>500){ 
      bpo.inSampleSize = 8; 
      b=BitmapFactory.decodeStream(inputStream, null,bpo); 
    } 
    else 
    { 
     bpo.inSampleSize=2; 
     b=BitmapFactory.decodeStream(inputStream, null,bpo); 
    } 
      return b; 
    } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 

}

이미지이다 사용 loadImageFromUrl() 메소드를 사용하거나 drawble 폴더에있는 경우 .xml은

입니다.
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:padding="2sp"> 


    <TextView 
android:id="@+id/title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="" 
android:layout_marginLeft="45px" 
/> 


    <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
> 

<ImageView 
android:id="@+id/image" 
android:layout_width="35px" 
android:layout_height="wrap_content" 
android:layout_marginLeft="2px"/> 


    </LinearLayout> 
    </RelativeLayout> 
+0

setadapter에서 nullpointer 예외가 발생했습니다. – user594720

관련 문제