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>
MainActivity.java
package in.blogspot.kantapp.customlistviewwithimageview; 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 = { "Android", "Birthday", "Cloud", "Smile", "Education" }; String[] locations = { "location1", "location2", "location3", "location4", "location5" }; int[] images = { R.drawable.android, R.drawable.birthday, R.drawable.cloude, R.drawable.smile, R.drawable.education }; }
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/birthday" android:layout_gravity="top|left" /> </RelativeLayout> </LinearLayout>
No comments:
Post a Comment