2013-02-15 74 views
0

Iam開發的應用程序來顯示圖像從url(這使我從json webservices這些網址)在gridview。當我直接給url時,代碼就是成功。問題在循環GridView與json android

下面是我的代碼:

package com.example.sample; 

import java.io.IOException; 
import java.io.InputStream; 
import java.lang.ref.SoftReference; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.Map; 
import java.util.WeakHashMap; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import com.example.sample.R; 


import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 



import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ListView; 

public class MainActivity extends Activity { 

    GridView list; 
    LazyAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     list=(GridView)findViewById(R.id.list); 
     adapter=new LazyAdapter(this, mStrings); 
     list.setAdapter(adapter); 
     adapter.imageLoader.clearCache(); 
     adapter.notifyDataSetChanged(); 


    } 


    private String[] mStrings={ 
      "http://www.1.png", 
      "http://www.2.png", 
      "http://www.3.png", 
      "http://www.4.png", 
      "http://www.5.png" 



    }; 
} 

這是正確顯示我的GridView這五個圖像。現在我的問題是如何從我的AsynchronousTask直接發送url列表到gridview。下面是我的AsynchronousTask code

private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>> { 

     protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { 
      InputStream in = entity.getContent(); 
      StringBuffer out = new StringBuffer(); 
      int n = 1; 
      while (n>0) { 
       byte[] b = new byte[4096]; 
       n = in.read(b); 
       if (n>0) out.append(new String(b, 0, n)); 
      } 
      return out.toString(); 
     } 

     @Override 
     protected ArrayList<String> doInBackground(Void... params) { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet("myurl"); 
     String text = null; 
     String result = null; 
     ArrayList<String> imageurl=new ArrayList<String>(); 
     String name=null; 
     String id; 

     try { 
       HttpResponse response = httpClient.execute(httpGet, localContext); 
       HttpEntity entity = response.getEntity(); 
       text = getASCIIContentFromEntity(entity); 





        JSONArray ja = new JSONArray(text) ; 
        // ITERATE THROUGH AND RETRIEVE CLUB FIELDS 
           int n = ja.length(); 
           for (int i = 0; i < n; i++) { 
            // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY 
           JSONObject jo = ja.getJSONObject(i); 

           imageurl.add(jo.getString("ImageUrl")); 
           name= jo.getString("ImageName"); 

            id= jo.getString("ImageId"); 



           } 
     } catch (Exception e) { 
         e.getLocalizedMessage(); 
        } 


     return imageurl; 


     }  


    protected void onPostExecute(ArrayList<String> results){ 
      if(results!=null){ 
    for(int j=0;j<results.size();j++){ 

      String[] mStrings={results.get(j)}; 
    } 
} 


} 

} 

我LazyAdapter代碼:

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private String[] data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.item, null); 


     ImageView image=(ImageView)vi.findViewById(R.id.image); 

     imageLoader.DisplayImage(data[position], image); 
     return vi; 
    } 
} 

如何發送imageurl(Arraylist)mStrings(string array)

回答

0

您的AsyncTask CALSS更改爲類似:

private class LongRunningGetIO extends AsyncTask <String, Void, ArrayList<String>> { 

......... 

    @Override 
     protected ArrayList<String> doInBackground(String... params) { 

      // run a loop here for the length of params which will download images one after other. 
.......... 
     } 
} 
+0

thanq Khawar Raza.I覺得上面也做的IAM沒有samething ???如你所說。 – kalyan 2013-02-16 04:52:59