Here is a Simple and very Easy tutorial if you want to show more than two lines in a single list item.
Steps:
- Create 2 layouts in res/layout folder. “main.xml” and “list_item.xml”
- Create a listview in main.xml (code given below)
- Create two textview in list_item.xml (code given below)
- Create MainActivity and set main.xml as its layout. (code given below)
- Run the application.
Code for main.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/user_thread_lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>Code for list_item.xml
<?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="vertical" > <TextView android:id="@+id/message_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="4dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/time_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout>Code For MainActivity
package com.example.tabs; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity implements OnClickListener { String[] names = new String[]{"A","B","C","D","E","F","G","H"}; String[] lastmessage = new String[]{"a","b","c","d","e","f","g","h"}; ListView messages_list; private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messages_list = (ListView)findViewById(R.id.user_thread_lv); simpleArray(); } private void simpleArray(){ String[] <strong>from</strong> = new String[] {"rowid", "col_1"}; int[] <strong>to</strong> = new int[] { R.id.message_tv, R.id.time_tv}; List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); for(int i = 0; i < names.length; i++){ HashMap<String, String> map = new HashMap<String, String>(); map.put("rowid", "" + names[i]); map.put("col_1", "" + lastmessage[i]); fillMaps.add(map); } adapter = new SimpleAdapter(this, fillMaps, R.layout.message_list_item, from, to); messages_list.setAdapter(adapter); } }Filed under: Adapter, Android Examples, Android Tutorial, Uncategorized | Tagged: Adapter, android, Android Example, Android source code, Listview, simple adapter, Source Code |
Leave a comment