Android ListView with multiple String arrays using Simple Adapter

Here is a Simple and very Easy tutorial if you want to show more than two lines in a single list item.

Steps:

  1. Create 2 layouts in res/layout folder. “main.xml” and   “list_item.xml”
  2. Create a listview in main.xml                (code given below)
  3. Create two textview in list_item.xml      (code given below)
  4. Create MainActivity and set main.xml as its layout. (code given below)
  5. Run the application.

source belowSource code:

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);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: