2013-05-18 38 views
0

在投票之前請耐心等待。我是Android的初學者。在嘗試了多種解決方案後,我無法解決此問題。使用Json在Listview(In Adapter)中顯示圖像

我想在列表視圖中設置下列項目中的圖像。我希望能夠在計劃適配器類的某處插入圖像代碼。

下面的代碼是我的日程表適配器

public class Schedule_ArrayAdapter extends ArrayAdapter<String> { 
    private final Activity activity; 
    private final String[] name, category, image; 
    Typeface colab, colab_bold, Bebas; 
    int selected = -1; 

public Schedule_ArrayAdapter(Activity activity, String[] name, String[] category, String[] image) { 
    super(activity, R.layout.schedule_item, category); 
    this.activity = activity; 
    this.name = name; 
    this.category = category; 
    this.image = image; 


    this.colab = Typeface.createFromAsset(activity.getAssets(), "ColabThi.otf"); 
    this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "ColabMed.otf"); 
    this.colab_bold = Typeface.createFromAsset(activity.getAssets(), "BebasNeue.otf"); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //get view and textview from xml 
    View rowView = inflater.inflate(R.layout.schedule_item, parent, false); 
    rowView.setBackgroundColor(0xffffffff); 
    LinearLayout background = (LinearLayout) rowView.findViewById(R.id.back); 
    if (selected != -1) { 
     if (selected == position) { 
      background.setBackgroundColor(0xffeaac4b); 
     } else { 
      background.setBackgroundColor(0xffffffff); 
     } 
    }else{ 
     background.setBackgroundColor(0xffffffff); 
    } 

    TextView TimeView = (TextView) rowView.findViewById(R.id.category); 
    TextView TitleView = (TextView) rowView.findViewById(R.id.title); 
    ImageView vi = (ImageView) rowView.findViewById(R.id.imgPreview); 

    GetImage getimage = new GetImage(); 
    getimage.execute(image); 

    //change names 
    TitleView.setText(category[position]); 
    TitleView.setTextSize(fontpercent_screenheight(3.5)); 
    TitleView.setPadding(dp(10), dp(5), dp(5), dp(0)); 
    TitleView.setTypeface(Bebas); 

    TimeView.setText(name[position]); 
    TimeView.setTextSize(fontpercent_screenheight(3.5)); 
    TimeView.setPadding(dp(10), dp(2), dp(5), dp(5)); 
    TimeView.setTypeface(colab); 

    return rowView; 
} 

以下是我ImageDownloader類

public class ImageDownloader { 
    private InputStream OpenHttpConnection(String image) throws IOException { 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(image); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 

    conn.setUseCaches(true); 
    HttpURLConnection httpConn = (HttpURLConnection) conn; 
    try { 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream(); 
     } 
    } catch (Exception ex) { 
     throw new IOException("Error connecting" + ex); 
    } 
    return in; 
} 

public Bitmap DownloadImage(String image) { 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(image); 
     bitmap = BitmapFactory.decodeStream(in); 
     in.close(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
    return bitmap; 
} 
} 

解析JSON和獲取數據的工作是正常的。在調試器中查看時,「image」數組也被提取。我如何在視圖中設置圖像? enter image description here

+0

嗨,你可以對你的問題添加的Android標籤?謝謝! – vincent

+0

所以你想創建一個帶有一些文本和類別的圖像的列表視圖?你想要做什麼? – cafebabe1991

+0

@ cafebabe1991是的。 Absoluteluy。只是我難以理解什麼代碼來實際顯示我的日程安排適配器 – Sam

回答

0

因此,在您的adapter的getView你應該做一些URL的請求,並使用

位圖位= BitmapFactory.decodeStream(inputstream);

現在設置這個在您的佈局中的ImageView

ImageView i=(ImageView)findViewById(R.id.imageiew); 
i.setImageResource(bitmap); 

以便顯示圖像。

+0

我的問題是我有一個字符串數組是URL。所以我的數組叫做image [],它有大約53個URL。' – Sam

+0

Hello Sam,爲什麼不把圖像位圖設置爲您的imageView vi?您將在getview方法中獲得數組位置。 – zangeed

+0

Hi @zangeed。對不起,我有點迷路。你能在我的代碼中指出它嗎?也許在我的代碼更正? – Sam

0

你也可以試試這個代碼

ImageView iv=(ImageView) findViewById(R.id.imageview); 
    try { 
     iv.setImageDrawable(grabImageFromUrl(Global.a5)); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

從URL抓取圖像的功能是

private Drawable grabImageFromUrl(String url) throws Exception { 
     return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"); } 
相關問題