我正在尝试将示例ArrayList
发送到第二个Activity
,以便可以在TableLayout
中显示。
我的MainActivity
班:
public class MainActivity extends AppCompatActivity {
public ArrayList<String> al = new ArrayList<>();
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
al.add("Add");
al.add("Sub");
Intent i = new Intent(view.getContext(),two.class);
i.putStringArrayListExtra("al", al);
startActivity(i);
}
});
}
}
activity_main
布局包含一个Button
,可打开下一个Activity
,其中包含TableLayout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:stretchColumns="0,1"
android:id="@+id/main_table" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_width="match_parent">
</TableLayout>
</LinearLayout>
在第二个Activity
中,将遍历ArrayList
以在TableLayout
中显示数据。
我猜ArrayList
不能正确传输到下一个Activity
。
public class two extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Intent i = getIntent();
ArrayList<String> al = i.getStringArrayListExtra("al");
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
TableRow tr_head = new TableRow(this);
//tr_head.setId(11);
// tr_head.setBackgroundColor(Color.GRAY);
int co=0;
for(String k:al)
{
co++;
TableRow tr = new TableRow(this);
tr.setId(co);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setText(k);
tv.setPadding(2, 0, 5, 0);
tr_head.addView(tv);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
答案 0 :(得分:0)
ArrayList发送正确,没有问题。
也不需要TableRow的LayoutParams,而是在TextView上添加LayoutParams。
如果已经为TableLayout定义了Layout,也不需要tr_head,则无需在此处提供LayoutParams:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
tl.addView(tr)会很好。 尝试使用下面的代码,对我来说这很好!
public class two extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Intent i = getIntent();
ArrayList<String> al = i.getStringArrayListExtra("al");
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
int co=0;
for(String k:al)
{
co++;
TableRow tr = new TableRow(this);
tr.setId(co);
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1.0f));
tv.setText(k);
tv.setPadding(2, 0, 5, 0);
tr.addView(tv);
tl.addView(tr);
}}}