Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 异步任务和上下文_Java_Android_Eclipse_Android Asynctask_Android Context - Fatal编程技术网

Java 异步任务和上下文

Java 异步任务和上下文,java,android,eclipse,android-asynctask,android-context,Java,Android,Eclipse,Android Asynctask,Android Context,这是我的代码,是一个带有此构造函数的自定义数组适配器 public class CustomArrayAdapter extends ArrayAdapter<String> { RowItems RowItems; List<RowData> DataList; public CustomArrayAdapter(Context context, int textViewId, String[] id, RowItems rowItems, List<RowD

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

public class CustomArrayAdapter extends ArrayAdapter<String> {

RowItems RowItems;
List<RowData> DataList;

public CustomArrayAdapter(Context context, int textViewId, String[] id, RowItems rowItems, List<RowData> listData)
公共类CustomArrayAdapter扩展了ArrayAdapter{
行项目行项目;
列表数据列表;
公共CustomArrayAdapter(上下文上下文、int textViewId、字符串[]id、行项目行项目、列表列表数据)
eclipse试图在异步任务中更改它 这是我的任务

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();
    }
私有类asyncTaskProduct扩展了AsyncTask{
字符串Url;
字符串结果;
上下文MyContex;
进展性帕金森病;
字符串[]id;
清单L;
ListView产品;
公共asyncTaskProduct(字符串url、上下文上下文、ListView lv){
Url=Url;
MyContex=contex;
lvProduct=lv;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
PD=新的ProgressDialog(MyContex);
设置消息(“文件下载…”);
PD.可设置可取消(假);
PD.setProgressStyle(ProgressDialog.STYLE_微调器);
PD.show();
}
@凌驾
受保护的布尔doInBackground(无效…arg0){
布尔状态=假;
getJsonResult Con=新建getJsonResult();
结果=Con.postForJson(Url);
如果(结果!=null){
状态=真;
Result=Result.substring(10,Result.length()-1);
}
返回状态;
};
受保护的void onPostExecute(布尔状态){
如果(州){
对于(int i=0;i
我的错误是lvProduct.setAdpter.eclips说构造函数未定义,并尝试将其更改为

CustomArrayAdapter(**asyncTaskProduct**, int textViewId, String[] id, RowItems rowItems, List<RowData> listData)
CustomArrayAdapter(**asyncTaskProduct**,int-textViewId,String[]id,RowItems-RowItems,List-listData)
为什么要将上下文更改为asyncTaskProduct

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));        
现在的问题是什么是您的上下文..如果您在活动(或扩展上下文的内容)中声明asynctask..那么您可以简单地使用ClassName。这


如果不是,那么最好在asynctask中传递一个上下文

asynctask不是活动或服务那样的扩展上下文。 线路应为:

lvProduct.setAdapter(MyContext,新的CustomArrayAdapter(this,R.id.lvRow\u tv\u id,id,new RowItems(),L));


顺便说一句:请用小写字母开始局部变量的变量名。因此
MyContext
实际上应该是
MyContext
。这对其他人来说更容易阅读。

tnx我将其改为MyContext。