/*附加获取联系人的游标数据*/
MainActivity.java
ListViewAdapter.java
GridViewAdapter.java
MyData.java
activity_main.xml
activity_list_item.xml
Cursor cursor=getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
ListAdapter listAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_1,cursor,new String[]{People.NAME},new int[]{android.R.id.text1});
MainActivity.java
package com.ct7_GridView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn_gridview,btn_listview;
private GridView gridView;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
}
/* 视图容器 */
public static final class ViewHolder {
public ImageView img;
public TextView title, info;
public Button viewBtn;
}
private void findView() {
// TODO Auto-generated method stub
btn_gridview=(Button) findViewById(R.id.btn_gridview);
btn_listview=(Button) findViewById(R.id.btn_listview);
btn_gridview.setOnClickListener(new BtnGridViewOnclickListener());
btn_listview.setOnClickListener(new BtnListViewOnclickListener());
gridView=(GridView) findViewById(R.id.gridview);
listView=(ListView) findViewById(R.id.listview);
gridView.setAdapter(new GridViewAdapter(this));
//gridView.setBackgroundColor(color.holo_red_light);
gridView.setOnItemClickListener(new GridViewOnItemClickListener());
listView.setAdapter(new ListViewAdapter(this));
//listView.setBackgroundResource(R.drawable.ic_action_search);
listView.setOnItemClickListener(new ListViewOnItemClickListener());
}
class BtnGridViewOnclickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listView.setVisibility(listView.GONE);
if(gridView.getVisibility()==gridView.VISIBLE){
gridView.setVisibility(gridView.GONE);
}else{
gridView.setVisibility(gridView.VISIBLE);
}
}
}
class BtnListViewOnclickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gridView.setVisibility(gridView.GONE);
if(listView.getVisibility()==listView.VISIBLE){
listView.setVisibility(listView.GONE);
}else{
listView.setVisibility(listView.VISIBLE);
}
}
}
class GridViewOnItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
toast("您选择了"+(position+1)+"张图片");
}
}
class ListViewOnItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
toast("您选择了"+(position+1)+"张图片");
}
}
private void toast(String text){
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
ListViewAdapter.java
package com.ct7_GridView;
import com.ct7_GridView.MainActivity.ViewHolder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Integer[] mImageIds =MyData.getImg();
public ListViewAdapter(Context c) {
mInflater=LayoutInflater.from(c);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.activity_list_item, null);
holder.img=(ImageView) convertView.findViewById(R.id.listItem_pic);
holder.title=(TextView) convertView.findViewById(R.id.listItem_title);
holder.info=(TextView) convertView.findViewById(R.id.listItem_content);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.img.setImageResource(mImageIds[position]);
holder.title.setText(mImageIds[position].toString());
holder.info.setText(mImageIds[position]+"--info");
return convertView;
}
}
GridViewAdapter.java
package com.ct7_GridView;
import com.ct7_GridView.MainActivity.ViewHolder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private Integer[] mImageIds =MyData.getImg();
public GridViewAdapter(Context context) {
// TODO Auto-generated constructor stub
mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.activity_list_item, null);
holder.img=(ImageView) convertView.findViewById(R.id.listItem_pic);
holder.title=(TextView) convertView.findViewById(R.id.listItem_title);
holder.info=(TextView) convertView.findViewById(R.id.listItem_content);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.img.setImageResource(mImageIds[position]);
holder.title.setText(mImageIds[position].toString());
holder.info.setText(mImageIds[position]+"--info");
return convertView;
}
}
MyData.java
package com.ct7_GridView;
public class MyData {
public static Integer[] getImg() {
Integer[] mImageIds = { R.drawable.android,
R.drawable.alarm, R.drawable.barcode_reader,
R.drawable.bluetooth, R.drawable.browser,
R.drawable.bump, R.drawable.calculator,
R.drawable.camcorder, R.drawable.calendar,
R.drawable.camera, R.drawable.compass,
R.drawable.deviantart, R.drawable.dialer,
R.drawable.dribbble, R.drawable.dropbox,
R.drawable.bluetooth, R.drawable.browser,
R.drawable.bump, R.drawable.calculator,
R.drawable.camcorder, R.drawable.calendar,
R.drawable.camera, R.drawable.compass,
R.drawable.deviantart, R.drawable.dialer,
R.drawable.dribbble, R.drawable.dropbox,
R.drawable.bluetooth, R.drawable.browser,
R.drawable.bump, R.drawable.calculator,
R.drawable.camcorder, R.drawable.calendar,
R.drawable.camera, R.drawable.compass,
R.drawable.deviantart, R.drawable.dialer,
R.drawable.dribbble, R.drawable.dropbox,
R.drawable.ic_launcher };
return mImageIds;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ListView"/>
<Button
android:id="@+id/btn_gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GridView"/>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="gone"
/>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</ListView>
</LinearLayout>
activity_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/listItem_pic"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:padding="2dip"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/listItem_pic"
android:layout_alignWithParentIfMissing="true"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/listItem_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="@+id/listItem_title"
android:textColor="#00ff00"
android:textSize="12px" />
<TextView
android:id="@+id/listItem_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@+id/listItem_content"
android:textColor="#0000ff"
android:textSize="14px" />
</LinearLayout>
</RelativeLayout>