传递给第二个 Activity 的 ArrayList 数据未显示在 TableLayout 中
ArrayList data passed to a second Activity is not showing in a TableLayout there
我正在尝试将示例 ArrayList
发送到第二个 Activity
,以便它可以在 TableLayout
.
中显示
我的MainActivity
class:
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));
}
}
}
ArrayList 正在正确发送,没有任何问题。
也不需要 TableRow 的 LayoutParams,改为在 TextView 上添加 LayoutParams。
tr_head 也不需要,如果您已经为 TableLayout 定义了 Layout,则无需在此处提供 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);
}}}
我正在尝试将示例 ArrayList
发送到第二个 Activity
,以便它可以在 TableLayout
.
我的MainActivity
class:
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));
}
}
}
ArrayList 正在正确发送,没有任何问题。
也不需要 TableRow 的 LayoutParams,改为在 TextView 上添加 LayoutParams。
tr_head 也不需要,如果您已经为 TableLayout 定义了 Layout,则无需在此处提供 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);
}}}