传递给第二个Activity的ArrayList数据未显示在TableLayout中

时间:2018-08-18 04:12:24

标签: android android-intent arraylist android-tablelayout

我正在尝试将示例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));
        }
    }   
}

1 个答案:

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