gpt4 book ai didi

java - 动态打印数组列表值

转载 作者:行者123 更新时间:2023-12-01 22:10:40 25 4
gpt4 key购买 nike

我正在开发一个应用程序,它接受用户的输入并将其存储在数组列表中以进行统计分析。我想在另一个 Activity 中以表格形式输出这些项目和值。

我可以通过 Intent 将值传递给下一个 Activity ,但不知道如何以表格形式动态添加 View 并显示数组列表的内容。

这是添加项目的代码:

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

这是将数据传递到另一个 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);
}
}

如何以表格形式显示商品、数量和瓦数?它们被声明为:

private List<String> mItem = new ArrayList<>();
private List<Integer> mQuantity = new ArrayList<>();
private List<Double> mRatingWattage = new ArrayList<>();

最佳答案

这里我发布了一个示例答案。

假设我通过 Intent 将 ArrayList 传递给另一个 Activity 并以表格形式显示它们。要求:

1 RecyclerView 和

2 RecyclerView Adapter 设置数据。

动态表 Activity

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

我的表适配器

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" />

希望有帮助。快乐编码。如果有帮助请投票。

完整代码是here谢谢。

关于java - 动态打印数组列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com