简体   繁体   English

动态打印 arraylist 值

[英]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.我想将 output 这些项目和值以表格形式放在另一个活动上。

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.假设我通过意图将 ArrayList 传递给另一个 Activity 并以表格形式显示它们。 Requirement:要求:

1 RecyclerView and 1 RecyclerView 和

2 RecyclerView Adapter to set data. 2 RecyclerView Adapter 设置数据。

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 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.完整代码在这里谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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