如何获取动态添加EditText的资源数值并求和,生成双切片饼图?
解决方案:获取动态EditText总和并实现饼图展示
1. 修正SecondActivity的视图绑定错误
你在addView方法里把动态添加的EditText错误绑定成了TextView,这会导致无法获取输入值,先修正这个问题:
private void addView(){ View newResource = getLayoutInflater().inflate(R.layout.row_add_data, null, false); ImageView imageClose = (ImageView)newResource.findViewById(R.id.Remove); // 修正:将TextView改为EditText EditText RN = (EditText) newResource.findViewById(R.id.RName); EditText RA = (EditText) newResource.findViewById(R.id.RAmount); AppCompatSpinner COP = (AppCompatSpinner) newResource.findViewById(R.id.ColourOP); ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, ColourList); COP.setAdapter(arrayAdapter); imageClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { removeView(newResource); } }); linearLayout.addView(newResource); }
2. 在ThirdActivity中计算资源总和
拿到传递过来的resultList后,遍历计算所有资源数量的总和:
public class ThirdActivity extends AppCompatActivity { RecyclerView recycle_results; ArrayList<Results> resultList = new ArrayList<>(); PieChart pieChart; // 后续饼图用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.thirdactivity); recycle_results = findViewById(R.id.res_results); pieChart = findViewById(R.id.pie_chart); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); recycle_results.setLayoutManager(linearLayoutManager); resultList = (ArrayList<Results>) getIntent().getExtras().getSerializable("list"); // 计算所有资源数量总和 int totalAmount = 0; for(Results res : resultList) { totalAmount += res.getResAmount(); } // 传入总和到适配器,用于显示占比 recycle_results.setAdapter(new ResultsAdapter(resultList, totalAmount)); // 初始化饼图 setupPieChart(totalAmount); } }
3. 修改ResultsAdapter显示占比
更新适配器,接收总和参数并计算每个资源的占比:
public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.ResultsView> { ArrayList<Results> resultList = new ArrayList<>(); int totalAmount; // 构造方法新增总和参数 public ResultsAdapter(ArrayList<Results> resultList, int totalAmount) { this.resultList = resultList; this.totalAmount = totalAmount; } @NonNull @Override public ResultsView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_results,parent,false); return new ResultsView(view); } @Override public void onBindViewHolder(@NonNull ResultsView holder, int position) { Results results = resultList.get(position); holder.resourceName.setText(results.getResName()); holder.resourceAmount.setText(String.valueOf(results.getResAmount())); // 计算并显示占比 float percentage = totalAmount == 0 ? 0 : (results.getResAmount() * 100f) / totalAmount; holder.resourceTotal.setText(String.format("占比: %.1f%%", percentage)); } @Override public int getItemCount() { return resultList.size(); } public class ResultsView extends RecyclerView.ViewHolder{ TextView resourceName, resourceAmount, resourceTotal; public ResultsView(@NonNull View itemView) { super(itemView); resourceName = (TextView) itemView.findViewById(R.id.resNa); resourceAmount = (TextView) itemView.findViewById(R.id.resAm); resourceTotal = (TextView) itemView.findViewById(R.id.resTot); } } }
4. 集成饼图展示(使用MPAndroidChart)
4.1 添加依赖
在Module级别的build.gradle中添加饼图库依赖:
dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' }
4.2 修改thirdactivity.xml布局
添加饼图控件到布局顶部:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ThirdActivity"> <LinearLayout android:id="@+id/layoutThird" android:layout_width="0dp" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.github.mikephil.charting.charts.PieChart android:id="@+id/pie_chart" android:layout_width="match_parent" android:layout_height="250dp" android:layout_margin="10dp"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/res_results" android:layout_width="match_parent" android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
4.3 配置饼图逻辑
在ThirdActivity中添加饼图初始化方法:
import com.github.mikephil.charting.components.Description; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieEntry; import com.github.mikephil.charting.utils.ColorTemplate; import android.graphics.Color; // ... 其他导入 private void setupPieChart(int totalAmount) { ArrayList<PieEntry> entries = new ArrayList<>(); for(Results res : resultList) { // 计算每个资源的占比 float percentage = totalAmount == 0 ? 0 : (res.getResAmount() * 100f) / totalAmount; entries.add(new PieEntry(percentage, res.getResName())); } // 配置数据集 PieDataSet dataSet = new PieDataSet(entries, "资源分布"); dataSet.setColors(ColorTemplate.MATERIAL_COLORS); dataSet.setValueTextSize(12f); dataSet.setValueTextColor(Color.BLACK); // 绑定数据到饼图 PieData data = new PieData(dataSet); pieChart.setData(data); // 设置图表描述 Description description = new Description(); description.setText("资源占比饼图"); pieChart.setDescription(description); // 刷新图表 pieChart.invalidate(); }
5. 验证功能
现在运行程序:
- 在SecondActivity动态添加资源,输入名称和数量,选择颜色
- 点击提交,ThirdActivity会显示资源列表(带占比)和对应的饼图
- 总和计算和饼图展示功能正常
内容的提问来源于stack exchange,提问作者FPStruck
相关产品推荐
相关产品推荐

