0

这是我的代码,带有此构造函数的自定义数组适配器

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.setAdapter。eclips 说构造函数未定义并尝试将其更改为

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

为什么将 Context 更改为 asyncTaskProduct?!

4

2 回答 2

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

在此语句中,第一个参数不是上下文。它是异步任务的一个实例。您需要将上下文作为参数传递。所以试试这个

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

现在的问题是 yourContext 是什么。如果您在活动中声明异步任务(或扩展上下文的东西)..那么您可以简单地使用 ClassName.this .....

如果没有,那么最好在 asynctask 中传递上下文

于 2013-10-07T15:51:22.243 回答
0

AsyncTask 不会像 Activity 或 Service 那样扩展上下文。该行应该是:

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

顺便说一句:请以小写字母开头局部变量的变量名。事实上MyContext应该如此myContext。这对其他人来说更容易阅读。

于 2013-10-07T15:57:04.337 回答