I am trying to create ListViewwith a custom layout. each item in ListViewshould look like below in item.xml.
in code i used
adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
but it does not work, because the constructor ArrayAdapter<T>takes the second parameter as intsomething like
android.R.layout.simple_list_item_1
and in my case this is a custom layout that
R.layout.listi_tems_layout
which adapter should i use or how to solve it. thank
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"/>
the 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);
rmaik source
share