【问题标题】:Print out ArrayList content in Android TableLayout在 Android TableLayout 中打印出 ArrayList 内容
【发布时间】:2012-05-28 13:14:06
【问题描述】:

我有这个 ArrayList:

 ArrayList<Double> debtList = datasource.debtList;
 ArrayList<Double> feeList = datasource.feeList;

如何在循环中的 TableLayout 中并排打印这两个列表(格式无关紧要)?这是布局:

 TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);

【问题讨论】:

    标签: android loops arraylist tablelayout


    【解决方案1】:

    如果arrayLists 大小不相等,我已经找到解决方案。

    这是我实现的逻辑:

    这是我的活动:

    public class TestStringActivity extends Activity {
    private ArrayList<String> input1 = new ArrayList<String>();
    private ArrayList<String> input2 = new ArrayList<String>();
    private TableRow row;
    private TableLayout inflate;
    private TextView txtcol1, txtcol2;
    
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
    
        //Populating the arrayList
        input1.add("1 ");
        input1.add("2 ");
        input1.add("3 ");
        input2.add(" Red");
        input2.add(" Blue");
        input2.add(" Green");
        input2.add(" White");
    
        inflate = (TableLayout) TestStringActivity.this
                .findViewById(R.id.mytable);
    
        for (int i = 0, j = 0; i < input1.size() || j < input2.size();) {
            row = new TableRow(TestStringActivity.this);
            txtcol1 = new TextView(TestStringActivity.this);
            if (input1.size() > i) {
                if ((input1.get(i) != null)) {
                    txtcol1.setText(input1.get(i));
                    i++;
                }
            } else {
                txtcol1.setText("");
            }
            row.addView(txtcol1);
    
            txtcol2 = new TextView(TestStringActivity.this);
            if ((input2.size() > j)) {
                if (input2.get(j) != null) {
                    txtcol2.setText(input2.get(j));
                    j++;
                }
            } else {
                txtcol2.setText("");
            }
            this.row.addView(txtcol2);
    
            inflate.addView(row);
    
        }
    
      }
    }
    

    这是我的表格布局 main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mytable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
    </TableLayout>
    

    如果 arrayLists 大小不相等,希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      好的,你有两个arraylistsdebtList和feeList,我假设arraylist都包含相同数量的元素,现在遍历这个列表,Create Table Row将两个textViews添加到table row,并将tablerow添加到tableLayout,这样你就可以执行以下操作:

      ArrayList<Double> debtList = datasource.debtList;
      ArrayList<Double> feeList = datasource.feeList;
      TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
      for(int i=0;i<debtList.size();i++)
      {
          TableRow row=new TableRow(this);
          double debt = debtList.get(i);
          double fee = feeList.get(i);
          TextView tvDebt=new TextView(this);
          tvDebt.setText(""+debt);
          TextView tvFee=new TextView(this);
          tvFee.setText(""+fee);
          row.addView(tvDebt);
          row.addView(tvFee);
          table.addView(row);
      }
      

      【讨论】:

      • 那太好了,谢谢。是的,顺便说一句,他们是平等的。
      【解决方案3】:

      假设ArrayLists 中的条目数等于TableLayout 中的单元格数,您可以尝试如下操作:

      int index = 0;
      TableLayout tableLayout = findViewById(R.id.table_layout);
      
      for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
          double debt = debtList.get(index);
          double fee = feeList.get(index);
      
          TableRow row = (TableRow) tableLayout.getChildAt(n);
          TextView cell = (TextView) row.findViewById(R.id.textview_cell);
      
          name.setText("debt = " + debt + ", fee = " + fee);
          index++;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-15
        • 2015-04-12
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多