【发布时间】: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