2015-10-03 13 views
1

我學習Android的,我有這個錯誤,我不知道如何在適配器getdata()固定爲ArrayList的

adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData()); 

我知道的參數解決

錯誤The constructor is undefined適配器construtcor數組列表的Gridviewadapter的構造函數應該被修復..但是如何?

public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> data) { 
super(context, layoutResourceId, data); 
this.layoutResourceId = layoutResourceId; 
this.mcontext =context; 
} 

這是代碼

protected void showList(){ 
    try { 
     JSONObject jsonObj = new JSONObject(myJSON); 
     peoples = jsonObj.getJSONArray(TAG_RESULTS); 

     for(int i=0;i<peoples.length();i++){ 
      JSONObject c = peoples.getJSONObject(i); 
      String id = c.getString(TAG_ID); 
      String url = c.getString(TAG_URL); 
      Listitem.add(new Listitem(id,url)); 
     } 

     adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData()); 

     list.setAdapter(adapter); 

的GetData()

公共無效的getData(){ 類GetDataJSON擴展的AsyncTask {

@Override 
    protected String doInBackground(String... params) { 
     DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
     HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php"); 

     // Depends on your web service 
     httppost.setHeader("Content-type", "application/json"); 

     InputStream inputStream = null; 
     String result = null; 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 

      inputStream = entity.getContent(); 
      // json is UTF-8 by default 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      result = sb.toString(); 
     } catch (Exception e) { 
      // Oops 
     } 
     finally { 
      try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
     } 
     return result; 
    } 

    @Override 
    protected void onPostExecute(String result){ 
     myJSON=result; 
     showList(); 
    } 
+0

錯誤是由於'getData()'函數引起的,請發佈包含'getData()'功能的整個代碼。 –

+0

@vipul_asri檢查我的編輯PLZ – Moudiz

回答

1

您的代碼應該是這樣的。

從服務器調用這條線從您的onCreate方法

GetDataJSON .execute(); 

讓你的數據,你的類將是這樣。

public class GetDataJSON extends AsyncTask{ 
@Override 
    protected String doInBackground(String... params) { 
     DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
     HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php"); 

     // Depends on your web service 
     httppost.setHeader("Content-type", "application/json"); 

     InputStream inputStream = null; 
     String result = null; 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 

      inputStream = entity.getContent(); 
      // json is UTF-8 by default 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      result = sb.toString(); 
     } catch (Exception e) { 
      // Oops 
     } 
     finally { 
      try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
     } 
     return result; 
    } 

    @Override 
    protected void onPostExecute(String result){ 
     myJSON=result; 
     showList(); 
    } 
} 

和你的方法showList會一樣。

protected void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      peoples = jsonObj.getJSONArray(TAG_RESULTS); 

      for(int i=0;i<peoples.length();i++){ 
       JSONObject c = peoples.getJSONObject(i); 
       String id = c.getString(TAG_ID); 
       String url = c.getString(TAG_URL); 
       Listitem.add(new Listitem(id,url)); 
      } 

      adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem); 

      list.setAdapter(adapter); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
+0

你可以檢查這個問題PLZ?其實現了這一個 http://stackoverflow.com/questions/32926948/what-is-the-cause-of-org-json-jsonexception-no-value-for-url – Moudiz