Some Times we may come across few situations where we need to display an image and information about the image beside it.
consider the following image as an example :
This task can be accomplished in many ways and am gonna discuss a way using Base Adapter.
In general a list view will contain only a single textview in each row which will be inflated (by default) for the number of items. But if we need another view other than a textview or an additional view we need to use an alternative.
This inflation is done by using an additional xml file which will be inflated onto the list view.
consider an xml file with a list view as below :
main.xml
<TextView
<ListView
And we are going to inflate the following xml file so that we can get an image and a textview in each row .
row.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="horizontal" >
<ImageView
android:id="@+id/row_IV_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/row_TV_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Now let us consider how to use the BaseAdapter class
we need to use a class which extends the baseAdapter class.
After extending the baseadapter class we need to add the 4 unimplemented methods of baseAdapter.
Consider the following code :
getCount() is used to identify the number of items that should be displayed in the list view.
We can consider a arraylist and specify the size of the arraylist so that the getView() will be called for all the list items , thereby displaying a row for each arraylist item.
Here we can see that we are inflating the row.xml file as a view and finally returning the view.
consider the entire code below : (I am not extending a ListActivity. Dont do it)
The above code also has an implementation of displaying a toast when a list item is clicked.
Any suggestions are welcomed.
Please do comment if any problem with the code.
consider the following image as an example :
This task can be accomplished in many ways and am gonna discuss a way using Base Adapter.
In general a list view will contain only a single textview in each row which will be inflated (by default) for the number of items. But if we need another view other than a textview or an additional view we need to use an alternative.
This inflation is done by using an additional xml file which will be inflated onto the list view.
consider an xml file with a list view as below :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/main_LV_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
row.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="horizontal" >
<ImageView
android:id="@+id/row_IV_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/row_TV_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Now let us consider how to use the BaseAdapter class
we need to use a class which extends the baseAdapter class.
After extending the baseadapter class we need to add the 4 unimplemented methods of baseAdapter.
Consider the following code :
- class CustomAdapter extends BaseAdapter {
- Context cont;
- public CustomAdapter(Context c) {
- // TODO Auto-generated constructor stub
- cont = c;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return 5;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- // TODO Auto-generated method stub
- View v = arg1;
- LayoutInflater lv = (LayoutInflater) cont
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- v = lv.inflate(R.layout.row, arg2, false);
- ImageView iv_android = (ImageView) v
- .findViewById(R.id.row_IV_android);
- TextView tv_label = (TextView) v.findViewById(R.id.row_TV_detail);
- tv_label.setText(" label is " + arg0);
- return v;
- }
- }
getCount() is used to identify the number of items that should be displayed in the list view.
We can consider a arraylist and specify the size of the arraylist so that the getView() will be called for all the list items , thereby displaying a row for each arraylist item.
Now how to use this above code to make it display the listview?
Its similar to setting an adapter for a listview but here the adapter will be an object of the class that extends the BaseAdapter.
i.e.., we use
listView.setAdapter(new CustomAdapter(this)) instead of
Once have a look at the line 34 :
listView.setAdapter(adapter);
Here we can see that we are inflating the row.xml file as a view and finally returning the view.
consider the entire code below : (I am not extending a ListActivity. Dont do it)
- import android.widget.Toast;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class CustomListsActivity extends Activity implements
- OnItemClickListener {
- /** Called when the activity is first created. */
- ListView listView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (ListView) findViewById(R.id.main_LV_list);
- listView.setAdapter(new CustomAdapter(this));
- listView.setOnItemClickListener(this);
- }
- class CustomAdapter extends BaseAdapter {
- Context cont;
- public CustomAdapter(Context c) {
- // TODO Auto-generated constructor stub
- cont = c;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return 5;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- // TODO Auto-generated method stub
- View v = arg1;
- LayoutInflater lv = (LayoutInflater) cont
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- v = lv.inflate(R.layout.row, arg2, false);
- ImageView iv_android = (ImageView) v
- .findViewById(R.id.row_IV_android);
- TextView tv_label = (TextView) v.findViewById(R.id.row_TV_detail);
- tv_label.setText(" label is " + arg0);
- return v;
- }
- }
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // TODO Auto-generated method stub
- Toast.makeText(this, "item clicked is " + arg2, Toast.LENGTH_SHORT)
- .show();
- }
- }
The above code also has an implementation of displaying a toast when a list item is clicked.
Any suggestions are welcomed.
Please do comment if any problem with the code.
Comments
Post a Comment