【问题标题】:Converting string into TextView?将字符串转换为TextView?
【发布时间】:2012-08-03 18:22:15
【问题描述】:

看看这段代码:

ArrayList<Object> row = data.get(position);

TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);

TextView storeText = new TextView(this);
idText.setText(row.get(1).toString());
tableRow.addView(storeText);

TextView maggiText = new TextView(this);
idText.setText(row.get(2).toString());
tableRow.addView(maggiText);

我必须手动创建一个TextView,将其设置为某个字符串,然后传递它。有没有一种方法可以直接将字符串传递给可以放入for 循环的行?

我正在寻找这种方法来解决可扩展性问题。

【问题讨论】:

  • 听起来像ArrayAdapter 是您所需要的。你可以覆盖getView()

标签: android arraylist


【解决方案1】:

///你复制粘贴的时候搞错了

//idText 在所有电视中使用

让我们按照下面的方式进行

TextView idText = new TextView(this);
        idText.setText(row.get(0).toString());
        tableRow.addView(idText);

        TextView storeText = new TextView(this);
        storeText.setText(row.get(1).toString());
        tableRow.addView(storeText);

        TextView maggiText = new TextView(this);
        maggiText.setText(row.get(2).toString());
        tableRow.addView(maggiText);

【讨论】:

    【解决方案2】:

    ArrayList row = data.get(position);

    for(int i=0; i<row.size(); i++)
    {
        TextView text= new TextView(this);
        idText.setText(row.get(i).toString());
        tableRow.addView(idText);
    }
    

    【讨论】:

    • 您不是连续创建三个组件,而是创建一个文本视图,并向其附加三个字符串。
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2019-10-10
    • 2019-01-05
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多