我正在尝试使用自定义布局创建ListView. listView中的每个项目应如下面发布的item.xml中所示.
在代码中,我用过
adapter = new ArrayAdapter(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
但它不起作用,因为ArrayAdapter< T>的构造函数了.接受第二个参数作为int之类的东西
android.R.layout.simple_list_item_1
,在我的情况下,它是自定义布局
R.layout.listi_tems_layout
我应该使用哪个适配器或如何解决这个问题.谢谢
项目:
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
android:id="@+id/tvlist_topic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
android:id="@+id/ivList_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/delete_icon"
android:contentDescription="icon to delete item from the Listview"
android:layout_weight="1"/>
android:id="@+id/cbList_hook"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:checked="false"
android:layout_weight="1"/>
mainlayout:
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
....
....
....
android:id="@+id/lvEco_topics"
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_below="@id/tvEco_topic"
android:layout_marginTop="30dp"
android:scrollbars="vertical"
android:divider="@android:drawable/alert_light_frame">
android:id="@+id/btEco_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lvEco_topics"
android:gravity="center"
android:text="Save"/>
码:
public class MainActivity extends Activity {
private ArrayList topicsList;
private ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
topicsList = new ArrayList();
topicsList.add("topic1");
topicsList.add("topic2");
topicsList.add("topic3");
topicsList.add("topic4");
topicsList.add("topic5");
topicsList.add("topic6");
adapter = new ArrayAdapter(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
如果觉得《android arrayadapter自定义 android – 如何向Arrayadapter添加自定义布局?》对你有帮助,请点赞、收藏,并留下你的观点哦!