Introduction
Normally Listview means only text values show in display but custom listview is used to show text with images.here using Inflater method its used to compare text values and image values into same row.thats why need to create one inflating xml layout file.
demo
Using the code
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:textColor="#000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"
android:layout_marginRight="10dp"
android:contentDescription="@string/app_name"/>
</LinearLayout>
MainActivity.java:
package com.example.list;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ListActivity {
private String[] listview_names = {"India","Bangladesh", "China","Indonesia" };
private int[] listview_images = {R.drawable.india,R.drawable.bangladesh,R.drawable.china,R.drawable.indonesia};
private ListView lv;
private ArrayList<String> array_sort;
private ArrayList<Integer> image_sort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv =(ListView) findViewById(android.R.id.list);
array_sort=new ArrayList<String> (Arrays.asList(listview_names));
image_sort=new ArrayList<Integer>();
for (int index = 0; index < listview_images.length; index++)
{
image_sort.add(listview_images[index]);
}
setListAdapter(new bsAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
Toast.makeText(getApplicationContext(), array_sort.get(position),
Toast.LENGTH_SHORT).show();
}
});
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
public int getCount()
{
// TODO Auto-generated method stub
return array_sort.size();
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return array_sort.get(position);
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return array_sort.size();
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) row.findViewById(R.id.title);
ImageView im = (ImageView) row.findViewById(R.id.imageview);
tv.setText(array_sort.get(position));
im.setImageDrawable(getResources().getDrawable(image_sort.get(position)));
return row;
}
}
}
1 comments:
Write commentsAwesome tutorial... i really like it.
ReplyEmoticonEmoticon