Donate Activity - Add a Donation
Here's the complete Report.java activity before we complete our penultimate step in this lab
package ie.app.activities;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import ie.app.R;
import ie.app.api.DonationApi;
import ie.app.models.Donation;
public class Report extends Base implements OnItemClickListener, OnClickListener {
ListView listView;
DonationAdapter adapter;
SwipeRefreshLayout mSwipeRefreshLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
listView = (ListView) findViewById(R.id.reportList);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.report_swipe_refresh_layout);
new GetAllTask(this).execute("/donations");
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new GetAllTask(Report.this).execute("/donations");
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View row, int pos, long id) {
new GetTask(this).execute("/donations", row.getTag().toString());
}
@Override
public void onClick(View view) {
if (view.getTag() instanceof Donation) {
onDonationDelete((Donation) view.getTag());
}
}
public void onDonationDelete(final Donation donation) {
String stringId = donation._id;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Donation?");
builder.setIcon(android.R.drawable.ic_delete);
builder.setMessage("Are you sure you want to Delete the \'Donation with ID \' \n [ "
+ stringId + " ] ?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
new DeleteTask(Report.this).execute("/donations", donation._id);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private class GetAllTask extends AsyncTask<String, Void, List<Donation>> {
protected ProgressDialog dialog;
protected Context context;
public GetAllTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog = new ProgressDialog(context, 1);
this.dialog.setMessage("Retrieving Donations List");
this.dialog.show();
}
@Override
protected List<Donation> doInBackground(String... params) {
try {
return (List<Donation>) DonationApi.getAll((String) params[0]);
} catch (Exception e) {
Log.v("ASYNC", "ERROR : " + e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Donation> result) {
super.onPostExecute(result);
app.donations = result;
adapter = new DonationAdapter(context, app.donations);
listView.setAdapter(adapter);
listView.setOnItemClickListener(Report.this);
mSwipeRefreshLayout.setRefreshing(false);
if (dialog.isShowing())
dialog.dismiss();
}
}
private class GetTask extends AsyncTask<String, Void, Donation> {
protected ProgressDialog dialog;
protected Context context;
public GetTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog = new ProgressDialog(context, 1);
this.dialog.setMessage("Retrieving Donation Details");
this.dialog.show();
}
@Override
protected Donation doInBackground(String... params) {
try {
return (Donation) DonationApi.get((String) params[0], (String) params[1]);
} catch (Exception e) {
Log.v("donate", "ERROR : " + e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Donation result) {
super.onPostExecute(result);
Donation donation = result;
Toast.makeText(Report.this, "Donation Data [ " + donation.upvotes + "]\n " +
"With ID of [" + donation._id + "]", Toast.LENGTH_LONG).show();
if (dialog.isShowing())
dialog.dismiss();
}
}
private class DeleteTask extends AsyncTask<String, Void, String> {
protected ProgressDialog dialog;
protected Context context;
public DeleteTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog = new ProgressDialog(context, 1);
this.dialog.setMessage("Deleting Donation");
this.dialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
return (String) DonationApi.delete((String) params[0], (String) params[1]);
} catch (Exception e) {
Log.v("donate", "ERROR : " + e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String s = result;
Log.v("donate", "DELETE REQUEST : " + s);
new GetAllTask(Report.this).execute("/donations");
if (dialog.isShowing())
dialog.dismiss();
}
}
class DonationAdapter extends ArrayAdapter<Donation> {
private Context context;
public List<Donation> donations;
public DonationAdapter(Context context, List<Donation> donations) {
super(context, R.layout.row_donate, donations);
this.context = context;
this.donations = donations;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_donate, parent, false);
Donation donation = donations.get(position);
ImageView imgDelete = (ImageView) view.findViewById(R.id.imgDelete);
imgDelete.setTag(donation);
imgDelete.setOnClickListener(Report.this);
TextView amountView = (TextView) view.findViewById(R.id.row_amount);
TextView methodView = (TextView) view.findViewById(R.id.row_method);
TextView upvotesView = (TextView) view.findViewById(R.id.row_upvotes);
amountView.setText("" + donation.amount);
methodView.setText(donation.paymenttype);
upvotesView.setText("" + donation.upvotes);
view.setTag(donation._id); // setting the 'row' id to the id of the donation
return view;
}
@Override
public int getCount() {
return donations.size();
}
}
}
Now, let's look at how we can add a new Donation and 'insert' it into the remote list of Donations, maintained on the Server.
Open your Donate.java Activity and have a look at the current implementations of donateButtonPressed() & reset()
public void donateButtonPressed (View view)
{
String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
int donatedAmount = amountPicker.getValue();
if (donatedAmount == 0)
{
String text = amountText.getText().toString();
if (!text.equals(""))
donatedAmount = Integer.parseInt(text);
}
if (donatedAmount > 0)
{
app.newDonation(new Donation(donatedAmount, method, 0));
progressBar.setProgress(app.totalDonated);
String totalDonatedStr = "$" + app.totalDonated;
amountTotal.setText(totalDonatedStr);
}
}
@Override
public void reset(MenuItem item)
{
app.totalDonated = 0;
amountTotal.setText("$" + app.totalDonated);
}
They add to our local list of Donations, and reset a simple field - we'll implement the reset feature in the final step so now, the first thing to do, is look at how we can refactor the donateButtonPressed() method to add a donation to our remote list and then update our total.
We're going to need another AsyncTask for this so see if you can complete this 'InsertTask' based on the previous Tasks you've implemented
private class InsertTask extends AsyncTask<Object, Void, String> {
protected ProgressDialog dialog;
protected Context context;
public InsertTask(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.dialog = new ProgressDialog(context, 1);
this.dialog.setMessage("Saving Donation....");
this.dialog.show();
}
@Override
protected String doInBackground(Object... params) {
String res = null;
try {
Log.v("donate", "Donation App Inserting");
}
catch(Exception e)
{
Log.v("donate","ERROR : " + e);
e.printStackTrace();
}
return res;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
And referring to the lecture material, complete the donateButtonPressed() method that will invoke the above Task.