2011-04-01 21 views
0

我已經創建了一個程序,它從服務器讀取文件以構建列表視圖。然後它在後臺加載一個圖像。現在圖像被加載到一個圖像視圖,以確保它的工作。如何將圖像加載到我的listview imageview行?如何在創建listvue後更改圖像?

public class Welcome extends ListActivity { 
    /** Called when the activity is first created. */ 
    // data to load from html file 
    ArrayList<String> images; 
    ArrayList<String> Links; 
    ArrayList<String> LinkName; 
    ArrayList<String> Price; 

    int y; 
    ArrayList<String> ted; 

    int i; 



    String[] items={"CASUAL","DRESSES","CAREER","OUTWEAR","FOOTWEAR", 
      "JEWELRY","ACCESSORIES"}; 
    private HttpClient client; 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // data read in can be from 1-10 
     images=new ArrayList(); 
     Links=new ArrayList(); 
     LinkName=new ArrayList(); 
     Price=new ArrayList(); 



     // load in arrays from a file on a web server 
     Loaddata();  

     // create a listvue based on the data loaded from the server 
    setListAdapter(new IconicAdapter(this)); 

     // load in biytmap 
    download(); 
    } // end function 

    class IconicAdapter extends ArrayAdapter { 
     Activity context; 

     IconicAdapter(Activity context) { 
      super(context, R.layout.row, LinkName); 

      this.context=context; 
     } 

     public View getView(int position, View convertView, 
               ViewGroup parent) { 
      LayoutInflater inflater=context.getLayoutInflater(); 
      View row=inflater.inflate(R.layout.row, null); 
      TextView label=(TextView)row.findViewById(R.id.label); 

      label.setText(LinkName.get(position)); 
      return(row); 
     } 
    } 


    private void download(){ 
    new AsyncTask<Void, Void, Bitmap>(){ 

     @Override 
     protected Bitmap doInBackground(Void... params) { 
     HttpClient client = new DefaultHttpClient(); 
     try { 
      String uri = "http://www.besttechsolutions.biz/icon.png"; 
      HttpGet request = new HttpGet(uri); 
      HttpResponse response = client.execute(request); 
      return BitmapFactory.decodeStream(response.getEntity().getContent()); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
     } 

     @Override 
     protected void onPostExecute(Bitmap image) { 
     if(image == null){ 
      Log.d("ted", "could not download image"); 
//    Toast.makeText(Main.this, "Download failed", Toast.LENGTH_LONG).show(); 
     } 
     else{ 
      // losd into ListView 
    //  ImageView myimage = (ImageView) findViewById(R.id.myimage); 
    //  myimage.setImageBitmap(image); 
     } 
     } 
    }.execute(); 
    } 
+0

你想顯示圖像在每一行像這樣的文字[圖像文字/標籤]? – 2011-04-01 13:09:39

回答

0
if(image == null){ 
      Log.d("ted", "could not download image"); 
      getAdapter().setImage(image); 
      //refresh 
      getAdapter().notifyDataSetChanged(); 
    } 

而且setImage方法添加到您的IconicAdapter

public void setImage(image){ 
    this.image = image; 
} 

您將需要一個字段image在適配器中,你應該在getView檢查並設定ImageView與它的情況下image!=null

+0

嗨,我很迷惑。我現在有一個iconicAdpter和一個getview methed來設置標籤。到目前爲止,每個行都會調用getview,並且在爲每行加載getview之後調用getview。我是初學者。 – 2011-04-04 14:43:03

+0

那麼問題是什麼?你試過我的解決方案嗎? getView將在getAdapter()。notifyDataSetChanged();後調用。 – ernazm 2011-04-04 15:02:04