How to build a CustomeListView in Android

How to make a ListView Customize:
step1: New java class “MyAdapter.java” > insert code1
step2: New layout “custome_item.xml” > insert code2
step3: New java class “Employee.java” > insert code3
step4: in MainActivity.java > insert code4

///////////////////////code 1////////////////////////////////////////////
public class MyAdapter extends ArrayAdapter<Employee> {
 Activity activity;
 int layoutId;
 ArrayList<Employee> listItems;

public MyAdapter(Activity context, int resource,ArrayList<Employee> objects) {
 super(context, resource,objects);
 // TODO Auto-generated method stub
 this.activity=context;
 this.layoutId= resource;
 this.listItems= objects;
 }
 @Override
 public View getView(int position, View v, ViewGroup parent){
 // TODO Auto-generated method stub
 LayoutInflater layoutInflater= activity.getLayoutInflater();
 v= layoutInflater.inflate(layoutId,null);
// TextView textView= (TextView) v.findViewById(R.id.listView_id);
 ImageView imageView1=(ImageView)v.findViewById(R.id.listView_image1);
 ImageView imageView2=(ImageView)v.findViewById(R.id.listView_image2);

Employee employee= listItems.get(position);
// textView.setText(employee.getId());
 imageView1.setBackgroundResource(employee.getImageView1());
 imageView2.setBackgroundResource(employee.getImageView2());
 return v;
 }
}

//////////////////////code 2///////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <!--<TextView-->
 <!--android:id="@+id/listView_id"-->
 <!--android:layout_width="50dp"-->
 <!--android:layout_height="50dp"-->
 <!--android:text="1"-->
 <!--android:textAlignment="center"/>-->
 <ImageView
 android:id="@+id/listView_image1"
 android:layout_width="150dp"
 android:layout_height="200dp"
 android:layout_alignParentTop="true"
 android:padding="5dp"
 android:layout_toRightOf="@+id/listView_id" />
 <ImageView
 android:id="@+id/listView_image2"
 android:layout_width="150dp"
 android:layout_height="200dp"
 android:layout_alignParentTop="true"
 android:padding="5dp"
 android:layout_toRightOf="@+id/listView_image1"
 />

</RelativeLayout>

/////////////////////////////code 3///////////////////////////////
public class Employee {
// String id;
 int imageView1,imageView2;

// public String getId() {
// return id;
// }
//
// public void setId(String id) {
// this.id = id;
// }

public int getImageView1() {
 return imageView1;
 }

public void setImageView1(int imageView1) {
 this.imageView1 = imageView1;
 }

public int getImageView2() {
 return imageView2;
 }

public void setImageView2(int imageView2) {
 this.imageView2 = imageView2;
 }
}

/////////////////////////code 4/////////////////////////////////////////
 int[] images={ R.drawable.test1, R.drawable.test2, R.drawable.test3 ,R.drawable.test4}
 private ImageView myWallpaper;
 private ListView listView;
 private ArrayList<Employee> arrayList= new ArrayList<Employee>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.main_listView);
 for (int i = 0; i < 6; i++) {
 Employee employee= new Employee();
// employee.setId(""+(i+1));
 employee.setImageView1(images[i]);
 employee.setImageView2(images[++i]);
 arrayList.add(employee);
 }
 MyAdapter adapter = new MyAdapter(this, R.layout.item_listview, arrayList);
 adapter.notifyDataSetChanged();
 listView.setAdapter(adapter);

/////////////////////////// END ////////////////////////////////////////
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s