Friday, 22 March 2013

Custom ListView with ImageView

Hi to all, Here i'm posting example on Custom ListView with ImageView.
This app is done by my Student Harish.

In this project some images were used those images you can download from internet and store them in drawable folder in your application.

ScreenShots:

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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

custom.xml:
<?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" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="25dp"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top|left"
         />
    
</RelativeLayout>

</LinearLayout>

MainActivity.java:

package com.example.customlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView1);
customadapter ca = new customadapter();
list.setAdapter(ca);

}

class customadapter extends BaseAdapter {

@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub

return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertview, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
convertview = inflater.inflate(R.layout.custom, null);
TextView tv = (TextView) convertview.findViewById(R.id.textView1);
TextView tv1 = (TextView) convertview.findViewById(R.id.textView2);
ImageView image = (ImageView) convertview
.findViewById(R.id.imageView1);
tv.setText(names[position]);
tv1.setText(locations[position]);
image.setImageResource(images[position]);

return convertview;
}

}

String[] names = { "name1", "name2", "name3", "name4", "name5" };
String[] locations = { "location1", "location2", "location3", "location4",
"location5" };
int[] images = { R.drawable.a_aa, R.drawable.ee_r, R.drawable.sm_a,
R.drawable.sma_ll, R.drawable.ww_w };
}