【问题标题】:Dynamically add Button to ArrayList in Android app在 Android 应用程序中将 Button 动态添加到 ArrayList
【发布时间】:2014-01-21 17:43:32
【问题描述】:

我有以下代码:

String[] citazioni = getResources().getStringArray(R.array.citazioni);
List<String> CitazioniOutput = new ArrayList<String>();
for (String value : citazioni) {
    CitazioniOutput.add(value+"\n<Button android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"@string/share\" android:onClick=\"Condividi("+value+")\" />");
}

ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,CitazioniOutput);

ListView listView= (ListView)findViewById(R.id.ListViewCitazioni);
listView.setAdapter(adapter);

我在每个 CitazioniOutput 项目中添加了文本“&lt;Button android:layout ...”。我想将此文本转换为实际的Button

@Raghunandan:是的,我想为每个项目添加分享按钮 :)

【问题讨论】:

  • 你想要列表视图中的每一行都有一个按钮吗??

标签: java android arraylist


【解决方案1】:

您可能需要使用自定义适配器创建自定义列表视图。为列表视图项使用自定义布局。在自定义布局文件中添加一个按钮。网上有很多教程如何做到这一点。 检查这些链接

Custom Listview - Android Hive

Listview - Vogella.com

【讨论】:

    【解决方案2】:

    您无法将文本值转换为新视图))

    如果您想在每一行添加按钮,请使用自定义视图: Custom Adapter for List View

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2022-06-19
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2014-08-03
      • 2016-10-06
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多