【问题标题】:using AsynTask inside Adapter class在 Adapter 类中使用 AsyncTask
【发布时间】:2017-05-15 08:18:25
【问题描述】:

我想通过单击列表的删除图标从自定义列表中删除我的项目。我使用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();
    }
}

java.lang.ClassCastException: android.app.Application 无法转换为 android app.Activity

我实际上并没有正确理解它:) 如果有人向我解释除了解决方案之外的确切问题,我会很高兴。

在实现 AsyncTask 后,我对代码进行了一些更改。但我试图在评论中解释它使用 ... // 或 /* */ 请帮我解决它。

提前致谢:)

【问题讨论】:

    标签: android json android-arrayadapter


    【解决方案1】:

    问题来了

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

    可能您传递的是 Application Context 而不是您的 Activity context

    【讨论】:

    • 谢谢,但实际上我对此的概念不是很清楚,您能帮我理解一下吗。我应该在何时何地使用它? :)
    • 哦,你是说这个,-final CustomListCheckoutAdapter adapter = new CustomListCheckoutAdapter( getApplicationContext(), R.layout.custom_list_final,arrayList , tv1 ); // 我从 Activity 类中调用的内容
    • 你指的是 - getApplicationContext() 吗?我现在应该做什么:)
    • 尝试只打电话给getContex()
    • getContex() 也不例外。由于我在 onPostExecute() 方法中使用适配器语句,因此列表仅通过 JSON 数据创建。另一个选项是 getBaseContex() 也不起作用。
    【解决方案2】:

    ((Activity) context)你应该传递 Activity 上下文!顺便说一句,你使用 runOnUiThread 太奇怪了,但是你在内部使用了一个异步任务......有更好的方法来处理你想要的。

    【讨论】:

    • 更好的方法是什么?你能分享一下吗?当然我会走更好的路:)
    • 我不知道你想做什么,但实际上你正在这样做 doOnMainthread(doOnotherthread(jsonstuff)) --> 这样做是一样的:doOnotherthread() 并且如果你需要一个“回调”使用接口或类似的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多