Wednesday, August 6, 2014

ListView with SimpleAdapter

main.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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@android:color/black"
        android:dividerHeight="1dp" />


</LinearLayout>


listrow.xml

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/app_name" >
    </ImageView>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/icon"
        android:textSize="15sp" >
    </TextView>

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@+id/icon"
        android:textSize="13sp" >
    </TextView>

</RelativeLayout>


MainActivity.xml

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

       private ListView list;

       @SuppressWarnings({ "unchecked", "rawtypes" })
       @Override
       protected void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             list = (ListView) findViewById(R.id.list);

             final List<Map> data = addSampleData();

             SimpleAdapter adapter = new SimpleAdapter(this,
                           (List<? extends Map<String, ?>>) data, R.layout.listrow,
                           new String[] { "key_icon", "key_name", "key_desc" }, new int[] {
                                        R.id.icon, R.id.name, R.id.desc });

             list.setAdapter(adapter);
             list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                 int position, long id) {

                           Toast.makeText(getApplicationContext(),
                                        data.get(position).get("key_name").toString(),
                                        Toast.LENGTH_LONG).show();
                    }
             });
       }

       @SuppressWarnings({ "unchecked", "rawtypes" })
       List<Map> addSampleData() {
             List<Map> list = new ArrayList<Map>();

             Map map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 1");
             map.put("key_desc", "This is Student 1");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 2");
             map.put("key_desc", "This is Student 2");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 3");
             map.put("key_desc", "This is Student 3");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 4");
             map.put("key_desc", "This is Student 4");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 5");
             map.put("key_desc", "This is Student 5");
             list.add(map);

             return list;
       }
}





No comments:

Post a Comment