0

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<>();
  • Read through the solutions mentioned [here](https://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a-listview-android) and you will get a few ideas of how to achieve your goal. – PGMacDesign Nov 01 '19 at 15:16
  • 3
    Opara, you have provided three statements and no questions. What is your question exactly? Also, most of the time, you are required to included complete, functional code so that others can see what you are doing wrong. I suggest you edit to include your code. – hfontanez Nov 01 '19 at 15:21
  • OK @hfontanez I'm on transit now. Will add code when I get home to my system. Thanks. – Opara Benjamin Nov 01 '19 at 15:36
  • As i understood. Your question is how to display Array list data received from another activity in your second Activity in Tabular form. Right?@OparaBenjamin. If i understood correct, you need to have GridView or TableLayout as a UI and some Adapter to display data in that UI. – CodeWithVikas Nov 02 '19 at 03:54
  • I will be posting sample code here soon for this. – CodeWithVikas Nov 02 '19 at 03:55
  • Alright thanks @vikasjha – Opara Benjamin Nov 02 '19 at 07:48

1 Answers1

0

Here i am posting a sample answer.

Let say i am passing an ArrayList via intent to another Activity and displaying them in tabular form. Requirement :

1 RecyclerView and

2 RecyclerView Adapter to set data.

DynamicTableActivity

import java.util.ArrayList;

public class DynamicTableActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_table);
        ArrayList<UserInput> userInputs = getIntent().getParcelableArrayListExtra("userInputs");

        Log.i(DynamicTableActivity.class.getSimpleName(),userInputs.toString());

        RecyclerView dynamicTable = findViewById(R.id.dynamic_table_view);

        dynamicTable.setLayoutManager(new LinearLayoutManager(this));
        dynamicTable.setAdapter(new MyTableAdapter(userInputs));
    }
}

MyTableAdapter

public class MyTableAdapter extends RecyclerView.Adapter<MyTableAdapter.MyViewHolder> {

    ArrayList<UserInput> userInputs;
    MyTableAdapter(ArrayList<UserInput> userInputs){
        this.userInputs = userInputs;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemViews = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_view,parent,false);
        return new MyViewHolder(itemViews);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {


        myViewHolder.cell1.setText(userInputs.get(position).getName());
        myViewHolder.cell2.setText(""+userInputs.get(position).getRoll_no());

    }

    @Override
    public int getItemCount() {
        Log.i(MyTableAdapter.class.getSimpleName(),"Size:"+userInputs.size());
        return userInputs.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView cell1,cell2;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            cell1 = itemView.findViewById(R.id.cell1);
            cell2 = itemView.findViewById(R.id.cell2);

        }
    }

}

activity_dynamic_table.xml

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".DynamicTableActivity"
    android:id="@+id/dynamic_table_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Hope It helps. Happy Coding. Please Vote if it helps.

Full code is here Thanks.

CodeWithVikas
  • 1,105
  • 9
  • 33
  • I'll try it out as soon as possible – Opara Benjamin Nov 02 '19 at 17:52
  • Thank you for your effort. But unfortunately the code you provided isn't complete and the link you provided is for an entirely different case, you might want to check the link again. I don't know the type of views you referred to from your adapter class. – Opara Benjamin Nov 02 '19 at 20:56
  • Ohh. Just give a try to clone the branch what i have provided in link and run. Your requirement is to get ArrayList from one activity via intent and display the array list data in another activity. right ? – CodeWithVikas Nov 03 '19 at 03:43
  • Adapter class referring to item-view of recycler-view.You must have item-view inside recycler-view to display data. – CodeWithVikas Nov 03 '19 at 04:01