GridView,ListView实例

1.建立MyGrid,MyList类

package com.archermind.activity;

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.widget.GridView;
import android.widget.SimpleAdapter;

public class MyGrid extends Activity
{
GridView gridView = null;
List<Map<String, Object>> contents = new ArrayList<Map<String, Object>>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mygrid);

gridView = (GridView) findViewById(R.id.grid);
for (int i = 0; i < 10; i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("PIC", R.drawable.pic);
map.put("TITLE", "test title");
contents.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, contents,
R.layout.griditem, new String[] { "PIC", "TITLE" }, new int[] {
R.id.griditem_pic, R.id.griditem_title, });

gridView.setAdapter(adapter);
}

}



package com.archermind.activity;

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.widget.ListView;
import android.widget.SimpleAdapter;

public class MyList extends Activity
{
ListView listView = null;
List<Map<String, Object>> contents = new ArrayList<Map<String, Object>>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);

listView = (ListView) findViewById(R.id.list);
for (int i = 0; i < 10; i++)
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("PIC", R.drawable.pic);
map.put("TITLE", "Test Title");
map.put("CONTENT", "Test Content");
contents.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,
(List<Map<String, Object>>) contents, R.layout.listitem,
new String[] { "PIC", "TITLE", "CONTENT" }, new int[] {
R.id.listitem_pic, R.id.listitem_title,
R.id.listitem_content });

listView.setAdapter(adapter);
}

}

 

2.建立4个XML:griditem.xml,listitem.xml,mygrid.xml,mylist.xml

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

<ImageView
android:id="@+id/griditem_pic"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_horizontal"
/>

<TextView
android:id="@+id/griditem_title"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_horizontal"
android:text
="test"
/>
</LinearLayout>

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

<ImageView
android:id="@+id/listitem_pic"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_alignParentTop
="true"
android:layout_alignParentBottom
="true"
android:src
="@drawable/pic" android:adjustViewBounds="true"
android:padding
="2dip"
/>
<TextView android:id="@+id/listitem_title"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_toRightOf
="@+id/listitem_pic"
android:layout_alignParentRight
="true"
android:layout_alignParentTop
="true"
android:layout_above
="@+id/listitem_content"
android:layout_alignWithParentIfMissing
="true"
android:gravity
="center_vertical"
android:text
="@+id/listitem_title"
android:textSize
="22px" />

<TextView android:id="@+id/listitem_content"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_toRightOf
="@+id/listitem_pic"
android:layout_alignParentBottom
="true"
android:layout_alignParentRight
="true"
android:singleLine
="true"
android:ellipsize
="marquee"
android:text
="@+id/item_content"
android:textSize
="14px" />
</RelativeLayout>

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


<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/grid"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:verticalSpacing
="10dp"
android:horizontalSpacing
="10dp"
android:numColumns
="auto_fit"
android:columnWidth
="90dp"
android:stretchMode
="columnWidth"
>

</GridView>
</LinearLayout>

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

<ListView
android:id="@+id/list"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
/>
</LinearLayout>


源码:http://files.cnblogs.com/kelei12399/MyDemo.zip

你可能感兴趣的:(ListView)