I am making a donation app. There is a amount picker where the user can choose the donate amount. And there is also a textView where the total sum donated can be seen if the user donates multiple times.
I'm trying to implement a donation history where each item in the list is $X. I used a list using a fragment on the same screen, using a pre-defined string array works perfectly well.
see image here, sorry I don't have enough rep to embed
My app crashed when I tried to do pass the donated amount to the fragment each time the user clicks on the donate button.
I can't figure out what's the problem. I'm guessing maybe the fragment needs to refresh with the new data, or maybe I'm passing data the wrong way. I'm completely stuck, not sure how to fix this. Any help is greatly appreciated, thanks!
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button donateButton;
private RadioGroup paymentMethod;
private ProgressBar progressBar;
private NumberPicker amountPicker;
private TextView totalTextView;
private int totalDonated = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
donateButton = (Button) findViewById(R.id.donateButton);
if (donateButton != null)
{
Log.v("Donate", "Really got the donate button");
}
paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
totalTextView = (TextView) findViewById(R.id.totalTextView);
// set the min and max values for the amount picker
amountPicker.setMinValue(0);
amountPicker.setMaxValue(1000);
progressBar.setMax(10000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void donateButtonPressed (View view)
{
int amount = amountPicker.getValue();
int radioId = paymentMethod.getCheckedRadioButtonId();
String method = "";
if (radioId == R.id.PayPal){
method = "PayPal";
} else {
method = "Direct";
}
TITLES[0] = "Donated " + totalDonated;
totalDonated = totalDonated + amount;
progressBar.setProgress(totalDonated);
String msg = "Total so far $"+totalDonated;
totalTextView.setText(msg);
Log.v("Donate", "Donate Pressed! with amount " + amount + ", method: " + method);
Log.v("Donate", "Current total " + totalDonated);
}
String[] TITLES = new String[3];
public String[] getDonationsArray() {
return TITLES.clone();
}
}
DonationHistory.java
public class DonationHistory extends ListFragment {
ListView listview;
View view;
MainActivity newDonationData = new MainActivity();
String[] Array = newDonationData.getDonationsArray();
public View onCreateView(LayoutInflater i,ViewGroup container,Bundle savedInstanceState)
{
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_1,R.id.text1,Array);
setListAdapter(adapter);
return super.onCreateView(i,container,savedInstanceState);
}
}