请我正在开发一个应用程序,该应用程序需要用户输入并将其存储在数组列表中以进行统计分析。我想在另一项活动中以表格形式输出这些项目和值。
我可以通过意图将值传递给下一个活动,但不知道如何以表格形式动态添加视图并显示数组列表的内容。
这是添加项目的代码:
time_period
这是用于将数据传递到另一个活动的代码:
df %>% {rename_at(., vars(sum), function(x) paste(pull(., time_period)[1], x, sep = "_"))}
# A tibble: 3 x 2
year_sum time_period
<dbl> <chr>
1 150 year
2 175 year
3 200 year
如何以表格形式显示物料,数量和瓦数? 它们声明为:
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("");
}
}
答案 0 :(得分:0)
我在这里发布示例答案。
假设我正在通过意图将ArrayList传递给另一个Activity,并以表格形式显示它们。 要求:
1 RecyclerView和
2 RecyclerView适配器以设置数据。
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" />
希望它会有所帮助。 编码愉快。如果有帮助,请投票。
完整代码为here 谢谢。