Android 在适配器类中使用AsynTask

Android 在适配器类中使用AsynTask,android,json,android-arrayadapter,Android,Json,Android Arrayadapter,我想通过单击列表的删除图标从自定义列表中删除我的项目。我使用了remove()和notifyDataSetChanged()方法将其从布局中删除。但当我试图通过JSON发送列表id时,我的应用程序崩溃了 public View getView(final int position, View convertView, ViewGroup parent) { .... // Some of normal code ... working fine .... holder.imv.set

我想通过单击列表的删除图标从自定义列表中删除我的项目。我使用了
remove()
notifyDataSetChanged()
方法将其从布局中删除。但当我试图通过JSON发送列表id时,我的应用程序崩溃了

public View getView(final int position, View convertView, ViewGroup parent) {
 ....
// Some of normal code ... working fine 
....
    holder.imv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            product2copy = product; // product is not getting recognized in Onpostexecution method ... coping it in the object declared as Instance 
            final int mitemid =Integer.parseInt(product.getId());

          //  boolean b = products.remove(product); I am putting it in post execution method

            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new CustomListCheckoutAdapter.ReadJSON3().execute("http://MyUrlhere/deleteditem.php?dish_id="+mitemid+"&uid=9155494233");    //1
                }
            });

/*  I am putting this in on onPostExecute method now ...
                if(b)
                {
                    notifyDataSetChanged(); 
                    Toast.makeText(getContext(),"The value has deleted from Local",Toast.LENGTH_LONG).show();
                    UpdateTotal();

                }
*/              
        }
    });

    return row;
}

private void UpdateTotal() {
    int i = getCount();
    int t = 0;
    String tt;
    for (int j=0;j<i ; j++){
        Checkout_product p = getItem(j);  
     tt= p.getPrice();
        if(tt==null)
            tt="0";
        t = t + Integer.parseInt(tt);
    }
    tvt.setText(Integer.toString(t));
}

private static class MyItemHolder{

    TextView tetName;
    TextView txtquant;
    TextView txtprice;

    ImageView imv;

}


// The ReadJSON2 class ..........

class ReadJSON3 extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        return readURL(params[0]);
    }

    @Override
    protected void onPostExecute(String content) {

        boolean b = products.remove(product2copy); //product - is not recognized

        if(b)
        {
            notifyDataSetChanged(); 
            Toast.makeText(getContext(),"The value has deleted from Local and in database too",Toast.LENGTH_LONG).show();
            UpdateTotal();

        }

       //super.onPostExecute(s);

    }
    private String readURL(String theURL) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(theURL);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}
public View getView(最终int位置、视图转换视图、视图组父视图){
....
//一些正常代码…工作正常
....
holder.imv.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
product2copy=product;//在Onpostexecution方法中无法识别产品…在声明为实例的对象中处理它
final int-mitemid=Integer.parseInt(product.getId());
//布尔b=products.remove(product);我将它放在执行后方法中
((活动)上下文).runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
新建CustomListCheckoutAdapter.ReadJSON3().execute(“http://MyUrlhere/deleteditem.php?dish_id=“+mitemid+”&uid=9155494233“;//1
}
});
/*我现在把这个放在onPostExecute方法中。。。
如果(b)
{
notifyDataSetChanged();
Toast.makeText(getContext(),“值已从本地删除”,Toast.LENGTH_LONG.show();
UpdateTotal();
}
*/              
}
});
返回行;
}
私有void UpdateTotal(){
int i=getCount();
int t=0;
字符串tt;
对于(intj=0;j问题就在这里

((Activity) context).runOnUiThread(...)

可能您正在传递的是
应用程序上下文
,而不是
活动上下文
((活动)上下文)你应该传递活动上下文!顺便说一句,你使用runOnUiThread很奇怪,但是你在内部使用asynctask…有更好的方法来处理你想要的内容。

谢谢,但实际上我对此的概念不太清楚。你能帮我理解它吗?我应该在何时何地使用它?:)哦,你的意思是,-final CustomListCheckoutAdapter adapter=new CustomListCheckoutAdapter(getApplicationContext(),R.layout.custom_list_final,arrayList,tv1);//我在活动类中调用的内容你是否指向-getApplicationContext()?我现在应该做什么:)尝试只调用
getContex()
getContex()也不例外。因为我在onPostExecute()方法中使用adapter语句,因为列表仅通过JSON数据创建。其他选项是getBaseContex()这也不起作用。更好的方法是什么?你能分享吗?当然我会选择更好的方法:)我不知道你想做什么,但实际上你在做这个doOnMainthread(DoonNotherThread(jsonstuff))-->也一样:DoonNotherThread(),如果你需要“回调”,使用一个接口或类似的东西