In this link teaches you to list comments (in my case news) using external database. There is a download of the project.
How can I delete a news item while selecting and still have a Dialog
appear asking if I want to delete news from the external database.
I notice the following error in the log: FATAL EXCEPTION: AsyncTask #2
Follow the project I'm using.
package br.com.transparencia.conexaoweb;
import java.util.ArrayList;
import java.util.List;
import android.widget.ArrayAdapter;
import java.util.HashMap;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import br.com.transparencia.R;
import br.com.transparencia.conexaoweb.ConexaoHttpClient;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ReadNoticiasPrefeitura extends ListActivity {
private ProgressDialog pDialog;
private Object json;
private static final String READ_COMMENTS_URL = " "; // URL LISTAR NOTICIAS
private static final String DELETE_COMMENTS_URL = " "; // URL DELETAR NOTICIAS
private static final String TAG_SUCCESS = "success";
private static final String TAG_TITULO = "titulo";
private static final String TAG_POSTS = "posts";
private static final String TAG_POST_ID = "id";
private static final String TAG_NOTICIA = "noticia";
// An array of all of our comments
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;
public JSONParser jsonParser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_noticias_prefeitura);
}
@Override
protected void onResume() {
super.onResume();
new LoadComments().execute();
}
public void sair(View v) {
finish();
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
String titulo = c.getString(TAG_TITULO);
String content = c.getString(TAG_NOTICIA);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITULO, titulo);
map.put(TAG_NOTICIA, content);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_TITULO, TAG_NOTICIA
}, new int[] { R.id.titulo, R.id.noticia });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
new DeleteCard().execute();
}
});
}
class DeleteCard extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadNoticiasPrefeitura.this);
pDialog.setMessage("Deleting Product...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Deleting product
* */
protected String doInBackground(String... args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("titulo", TAG_TITULO));
JSONObject json = jsonParser.makeHttpRequest(
DELETE_COMMENTS_URL, "POST", params);
Log.d("DELETAR", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent i = getIntent();
setResult(100, i);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
/* ----------------------------------------------------------------------------------------*/
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadNoticiasPrefeitura.this);
pDialog.setMessage("Carregando...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
/* ########################################################################################################## */
public void mensagemExibir(String titulo, String texto)
{
AlertDialog.Builder mensagem = new AlertDialog.Builder(ReadNoticiasPrefeitura.this);
mensagem.setTitle(titulo);
mensagem.setMessage(texto);
mensagem.setNeutralButton("OK", null);
mensagem.show();
}
}