Please I am working on an app that takes input from user and stores them in array list for statistical analysis. I want to output these items and values in tabular form on another activity.
I can pass the values via intent to the next activity but don't know how to dynamically add the views in tabular form and display the contents of the array lists.
This is the code for adding items:
public void addItem(View view) {
TextView itemTextView = findViewById(R.id.item_text);
TextView quantityTextView = findViewById(R.id.quantity_text);
TextView ratingTextView = findViewById(R.id.watt_rating_text);
TextView hoursTextView = findViewById(R.id.hour_text);
TextView powerFactorTextView = findViewById(R.id.power_factor_text);
String itemValue = itemTextView.getText().toString();
String quantityValue = quantityTextView.getText().toString();
String ratingValue = ratingTextView.getText().toString();
String hoursValue = hoursTextView.getText().toString();
String powerFactorValue = powerFactorTextView.getText().toString();
if (itemValue.isEmpty() || quantityValue.isEmpty() || ratingValue.isEmpty() || hoursValue.isEmpty() || powerFactorValue.isEmpty()){
Toast.makeText(this, "You must input all fields", Toast.LENGTH_LONG).show();
}
else {
mSolarCalc.addItem(itemValue);
mSolarCalc.addQuantity(Integer.parseInt(quantityValue));
mSolarCalc.addRating(Double.parseDouble(ratingValue));
mSolarCalc.addHours(Double.parseDouble(hoursValue));
mSolarCalc.addPowerFactor(Double.parseDouble(powerFactorValue));
TextView itemUpdate = findViewById(R.id.item_update);
String itemUpdateString = itemUpdate.getText().toString();
int itemUpdateInt = Integer.parseInt(itemUpdateString);
mItemUpdatedInt = ++itemUpdateInt;
itemUpdate.setText(Integer.toString(mItemUpdatedInt));
itemTextView.setText("");
quantityTextView.setText("");
ratingTextView.setText("");
hoursTextView.setText("");
powerFactorTextView.setText("");
}
}
This is the code for passing the data to another activity:
public void calculate(View view) {
if (mSolarCalc.getItem().size() == 0){
Toast.makeText(this, "You must add at least one item", Toast.LENGTH_LONG).show();
}
else {
mSolarCalc.calculateApparentPower();
mSolarCalc.calculatePower();
mSolarCalc.calculateEnergy();
double totalPower = 0;
double totalEnergy = 0;
for (int count = 0; count < mSolarCalc.getItem().size(); count++){
totalPower = totalPower + mSolarCalc.getPower().get(count);
totalEnergy = totalEnergy + mSolarCalc.getEnergy().get(count);
}
Intent intent = new Intent(this, SummaryActivity.class);
intent.putExtra(SummaryActivity.POWER_RESULT, totalPower);
intent.putExtra(SummaryActivity.ENERGY_RESULT, totalEnergy);
intent.putExtra(SummaryActivity.ITEM_LIST, (Serializable) mSolarCalc.getItem());
intent.putExtra(SummaryActivity.QUANTITY_LIST, (Serializable) mSolarCalc.getQuantity());
intent.putExtra(SummaryActivity.WATTAGE_LIST, (Serializable) mSolarCalc.getRatingWattage());
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}
How can I display Item, Quantity and Wattage in tabular form? They are declared as:
private List<String> mItem = new ArrayList<>();
private List<Integer> mQuantity = new ArrayList<>();
private List<Double> mRatingWattage = new ArrayList<>();