本文介绍了如何向 Arrayadapter 添加自定义布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义布局创建一个 ListView.listView 中的每一项都应该像下面发布的 item.xml 所示.

I am trying to create a ListView with customized layout. each item in the listView should look like as shown in the item.xml posted below.

在代码中,我使用了

adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);

但它不起作用,因为 ArrayAdapter 的构造函数接受第二个参数作为 int 类似

but it is not working because the constructor of the ArrayAdapter<T> accepts the second parameter as int something like

android.R.layout.simple_list_item_1

,就我而言,它是自定义布局,即

, and in my case it is customized layout which is

R.layout.listi_tems_layout

我应该使用哪个适配器或如何解决这个问题.谢谢

which adapter should i use or how to solve this. thanks

项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">

<TextView
    android:id="@+id/tvlist_topic"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<ImageView
    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"/>
<CheckBox
    android:id="@+id/cbList_hook"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:checked="false"
    android:layout_weight="1"/>

主要布局:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
....
....
....

<ListView
    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"></ListView>
<Button
    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<String> topicsList;
private ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    topicsList = new ArrayList<String>();
    topicsList.add("topic1");
    topicsList.add("topic2");
    topicsList.add("topic3");
    topicsList.add("topic4");
    topicsList.add("topic5");
    topicsList.add("topic6");

    adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);

推荐答案

当不自定义 ArrayAdaptergetView 方法时,自定义布局需要一个带有 android:id="@android:id/text1" 的 TextViewid 并在一个 TextView 中显示值.

When not customizing getView method of ArrayAdapter then custom layout require one TextView with android:id="@android:id/text1" id and show value in one TextView.

要使用当前代码运行应用程序,请在 R.layout.listi_tems_layout 中为 TextView 添加 android:id="@android:id/text1"> 布局.

To run application with current code add android:id="@android:id/text1" for TextView in R.layout.listi_tems_layout layout.

因为 R.layout.listi_tems_layout 布局也包含其他带有 TextView 的视图,所以通过扩展 ArrayAdapter 类来创建自定义适配器以访问其他视图.

Because R.layout.listi_tems_layout layout contains other views also with TextView so create custom Adapter by extending ArrayAdapter class to access other views also.

参见以下示例:自定义 ArrayAdapter for一个 ListView (Android)

这篇关于如何向 Arrayadapter 添加自定义布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:41