简体   繁体   中英

Dynamically printing arraylist values

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<>();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM