Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

android - how to add customised layout to Arrayadapter?

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.

in the code, i used

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

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

Item:

<?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"/>

mainlayout:

<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"/>

code:

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ArrayAdapter accepts the second parameter as int something like android.R.layout.simple_list_item_1

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.

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

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.

See following example:Custom ArrayAdapter for a ListView (Android)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...