activity class:
package com.nil.gallery;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class GalleryEx extends Activity {
/** Called when the activity is first created. */
Gallery gallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
gallery = (Gallery) findViewById(R.id.gallery);
// String array holding the values
final ArrayList<S> s = GetSearchResults();
// Array adapter to display our values in the gallery control
gallery.setAdapter(new MyCustomBaseAdapter1(this, s));
}
ArrayList<S> GetSearchResults() {
ArrayList<S> results = new ArrayList<S>();
S sr1 = new S();
sr1.setName("New York");
sr1.setCityState("Newyork");
sr1.setMail("a@a.c");
sr1.setPhone("123");
results.add(sr1);
sr1 = new S();
sr1.setName("Los Angeles");
sr1.setCityState("California");
sr1.setMail("a@a.c");
sr1.setPhone("456");
results.add(sr1);
sr1 = new S();
sr1.setName("Chicago");
sr1.setCityState("Illinios");
sr1.setMail("a@a.c");
sr1.setPhone("789");
results.add(sr1);
sr1 = new S();
sr1.setName("Houstan");
sr1.setCityState("Texas");
sr1.setMail("a@a.c");
sr1.setPhone("112");
results.add(sr1);
sr1 = new S();
sr1.setName("Dallas");
sr1.setCityState("Texas");
sr1.setMail("a@a.c");
sr1.setPhone("159");
results.add(sr1);
sr1 = new S();
sr1.setName("Houstan");
sr1.setCityState("Texas");
sr1.setMail("a@a.c");
sr1.setPhone("963");
results.add(sr1);
sr1 = new S();
sr1.setName("Detroirt");
sr1.setCityState("Michigan");
sr1.setMail("a@a.c");
sr1.setPhone("357");
results.add(sr1);
return results;
}
}
adapterclass:
package com.nil.gallery;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
public class MyCustomBaseAdapter1 extends BaseAdapter {
private static final int position = 0;
private static ArrayList<S> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter1(Context c, ArrayList<S> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(c);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int arg0) {
return searchArrayList.get(position);
}
public long getItemId(int arg0) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.main, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.hi1);
holder.txtCityState = (TextView) convertView.findViewById(R.id.hi2);
holder.txtmail = (TextView) convertView.findViewById(R.id.hi3);
holder.phone = (TextView) convertView.findViewById(R.id.hi4);
if(position==0){
}
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position)
.getCityState());
holder.phone.setText(searchArrayList.get(position).getPhone());
holder.txtmail.setText(searchArrayList.get(position).getMail());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtmail;
TextView phone;
}
}
setgetclass:
package com.nil.gallery;
public class S {
private String name = "";
private String cityState = "";
private String mail = "";
private String phone = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCityState() {
return cityState;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
main.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">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clipChildren="false"
android:spacing="20dp" android:gravity="top" />
<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="fill_parent" android:layout_height="150dp"
android:orientation="vertical" android:background="#909122">
<TextView android:layout_width="200dp" android:textSize="20dp" android:id="@+id/hi1"
android:layout_height="wrap_content" android:gravity="right" />
<TextView android:layout_width="fill_parent" android:id="@+id/hi2"
android:layout_height="wrap_content" android:gravity="right" />
<TextView android:layout_width="fill_parent" android:id="@+id/hi3"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent" android:id="@+id/hi4"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>