MainActivity.java
package com.customlistviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listview;
int[] img_resource = {R.drawable.apple,R.drawable.banana,
R.drawable.cherry,R.drawable.mango,R.drawable.orange,R.drawable.strawberry,R.drawable.tomato};
String[] name = {"Apple","Banana","Cherry","Mango","Orange","Strawberry","Tomato"};
String[] Qty = {"100 kg","150 kg","300 kg","250 kg","500 kg","300 kg", "220 kg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.main_list_view);
FruitAdapter adapter = new FruitAdapter(getApplicationContext(),R.layout.row_layout);
listview.setAdapter(adapter);
int i = 0;
for(String Name : name )
{
FruitClass obj = new FruitClass(img_resource[i],Name, Qty[i]);
adapter.add(obj);
i++;
}
}
}
FruitAdapter.java
package com.customlistviewdemo;
import java.util.ArrayList;
import java.util.List;
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{
private List list= new ArrayList();
public FruitAdapter(Context context, int resource) {
super(context, resource);
// TODO Auto-generated constructor stub
}
public void add(FruitClass object) {
// TODO Auto-generated method stub
list.add(object);
super.add(object);
}
static class ImgHolder
{
ImageView IMG;
TextView NAME;
TextView QTY;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row;
row = convertView;
ImgHolder holder;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout,parent,false);
holder = new ImgHolder();
holder.IMG = (ImageView) row.findViewById(R.id.fruit_img);
holder.NAME = (TextView) row.findViewById(R.id.fruit_name);
holder.QTY = (TextView) row.findViewById(R.id.fruit_qty);
row.setTag(holder);
}
else
{
holder = (ImgHolder) row.getTag();
}
FruitClass FR = (FruitClass) getItem(position);
holder.IMG.setImageResource(FR.getFruit_resource());
holder.NAME.setText(FR.getFruit_name());
holder.QTY.setText(FR.getFruit_qty());
return row;
}
}
FruitClass.java
package com.customlistviewdemo;
public class FruitClass {
private int fruit_resource;
private String fruit_name;
private String fruit_qty;
public FruitClass(int fruit_resource, String fruit_name, String fruit_qty) {
super();
this.setFruit_resource(fruit_resource);
this.setFruit_name(fruit_name);
this.setFruit_qty(fruit_qty);
}
public int getFruit_resource() {
return fruit_resource;
}
public void setFruit_resource(int fruit_resource) {
this.fruit_resource = fruit_resource;
}
public String getFruit_name() {
return fruit_name;
}
public void setFruit_name(String fruit_name) {
this.fruit_name = fruit_name;
}
public String getFruit_qty() {
return fruit_qty;
}
public void setFruit_qty(String fruit_qty) {
this.fruit_qty = fruit_qty;
}
}
activit_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/main_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
></ListView>
</RelativeLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:focusableInTouchMode="true"
>
<ImageView
android:id="@+id/fruit_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/fruit_img"
android:layout_centerVertical="true"
android:paddingTop="10dp"
/>
<TextView
android:id="@+id/fruit_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingTop="10dp"
android:paddingRight="10dp"
/>
</RelativeLayout>
Comments
Post a Comment