package com.example.listviewdemo; import java.util.ArrayList; import java.util.List; import com.example.listviewdemo.adapter.FruitAdapter; import com.example.listviewdemo.entity.Fruit; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private GridView gvFruits; private List<Fruit> fruitList = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gvFruits = (GridView) findViewById(R.id.gvFruit); getData(); FruitAdapter fruitAdapter = new FruitAdapter(this, R.layout.listview_item, fruitList); gvFruits.setAdapter(fruitAdapter); gvFruits.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // TODO Auto-generated method stub Fruit fruit = fruitList.get(position); Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_LONG); } }); } private void getData(){ int[] imageIds = {R.drawable.apple_pic,R.drawable.banana_pic, R.drawable.cherry_pic,R.drawable.grape_pic, R.drawable.mango_pic,R.drawable.orange_pic, R.drawable.pear_pic,R.drawable.pineapple_pic, R.drawable.strawberry_pic,R.drawable.watermelon_pic}; String[] names = {"苹果","香蕉","樱桃","葡萄","芒果","橘子","梨","菠萝","草莓","西瓜"}; for(int i = 0;i<imageIds.length;i++){ fruitList.add(new Fruit(imageIds[i], names[i])); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
FruitAdapter.java
package com.example.listviewdemo.adapter; import java.util.List; import com.example.listviewdemo.R; import com.example.listviewdemo.entity.Fruit; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view ; ViewHolder viewHolder; if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView)view.findViewById(R.id.ivImage); viewHolder.fruitName = (TextView)view.findViewById(R.id.tvName); view.setTag(viewHolder); } else{ view = convertView; viewHolder = (ViewHolder)view.getTag(); } Fruit fruit = getItem(position); viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } } class ViewHolder{ ImageView fruitImage; TextView fruitName; }
package com.example.listviewdemo.entity; public class Fruit { private int imageId; private String name; public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Fruit(int imageId, String name) { super(); this.imageId = imageId; this.name = name; } }
<?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" > <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:src="@drawable/apple_pic" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <GridView android:id="@+id/gvFruit" android:layout_width="match_parent" android:layout_height="wrap_content" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="4" > </GridView> </RelativeLayout>