2013-10-07 39 views
0

這是我的代碼的自定義陣列適配器與此構造的AsyncTask和上下文

public class CustomArrayAdapter extends ArrayAdapter<String> { 

RowItems RowItems; 
List<RowData> DataList; 

public CustomArrayAdapter(Context context, int textViewId, String[] id, RowItems rowItems, List<RowData> listData) 

和eclipse改掉,以改變它在我的AsyncTask ,這是我的AsyncTask

private class asyncTaskProduct extends AsyncTask<Void, Void, Boolean>{ 
    String Url; 
    String Result; 
    Context MyContex; 
    ProgressDialog PD; 
    String[] id; 
    List<RowData> L; 
    ListView lvProduct; 
    public asyncTaskProduct(String url, Context contex,ListView lv) { 
     Url = url; 
     MyContex = contex; 
     lvProduct = lv;   
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     PD = new ProgressDialog(MyContex); 
     PD.setMessage("File downloading ..."); 
     PD.setCancelable(false); 
     PD.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     PD.show(); 
    } 

    @Override 
    protected Boolean doInBackground(Void... arg0) { 
     boolean State = false; 
     getJsonResult Con = new getJsonResult(); 
     Result = Con.postForJson(Url); 
     if (Result != null){ 
      State = true; 
      Result = Result.substring(10, Result.length() - 1); 
     } 
     return State; 
    }; 

    protected void onPostExecute(Boolean State) { 
     if (State) { 
      for (int i = 0; i < L.size(); i++) { 
       id [i]=String.valueOf(i); 
      } 
      lvProduct.setAdapter(new CustomArrayAdapter(this,R.id.lvRow_tv_ID, id, new RowItems(), L));     
      Toast.makeText(MyContex,Result, Toast.LENGTH_SHORT).show(); 
     }else { 
      Toast.makeText(MyContex,"Error", Toast.LENGTH_SHORT).show(); 
     } 
     PD.cancel(); 
    } 

我的錯誤是lvProduct .setAdpter。說文摘構造是不確定的並嘗試將其更改爲

CustomArrayAdapter(**asyncTaskProduct**, int textViewId, String[] id, RowItems rowItems, List<RowData> listData) 

爲什麼改變語境asyncTaskProduct?!

回答

0
new CustomArrayAdapter(this,R.id.lvRow_tv_ID, id, new RowItems(), L)); 

在此聲明中,第一個參數不是上下文。它是asynctask的一個實例。您需要傳遞一個上下文作爲參數。所以,試試這個,而不是

new CustomArrayAdapter(yourcontext ,R.id.lvRow_tv_ID, id, new RowItems(), L));   

現在的問題是什麼是yourContext ..如果您聲明(擴展方面或某事),一個活動中的AsyncTask ..然後你可以簡單地使用ClassName.this .... 。

如果沒有,那麼最好在的AsyncTask

+0

tnx我將其更改爲myContext。 –

+0

歡迎您:) – stinepike

0

一個的AsyncTask不extendeing語境諸如活動或服務確實傳遞的上下文。 行應該是:

lvProduct.setAdapter(MyContext, new CustomArrayAdapter(this,R.id.lvRow_tv_ID, id, new RowItems(), L));

BTW:請開始與小寫局部變量的變量名。所以MyContext其實應該是myContext。這對其他人來說更容易閱讀。