Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize listview using baseadapter

I wanna create a customized ListView like this:

TextView+ImageView in ListView

I think that I have to use BaseAdapter but I have no idea about it.

like image 702
Mohsen Bahman Avatar asked May 02 '13 08:05

Mohsen Bahman


People also ask

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

How to use the baseadapter with listview in Android?

To use the BaseAdapter with a ListView, a concrete implementation the BaseAdapter class that implements the following methods must be created: Before we create our custom BaseAdapter implementation, we need to create the layout for the ListView row and also a model for the items in the ListView.

What is the use of base adapter in listview?

BaseAdapter with Android ListView ListView is a ViewGroup that displays a list of vertically scrollable items. The list items are automatically inserted into the list using an adapter that is connected to a source, such as an array or a database query, and each item is converted into a row in the ListView.

How to display a custom list item in a listview?

Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend base adapter in that. Base Adapter can be extended to create a custom Adapter for displaying a custom list item. Important Note: ArrayAdapter is also an implementation of BaseAdapter.

Can I use an arrayadapter with a listview?

If your data source is an ArrayList or array, we can also use the ArrayAdapter construct as an alternative. Note that ArrayAdapter itself extends from BaseAdapter. To use the BaseAdapter with a ListView, a concrete implementation the BaseAdapter class that implements the following methods must be created:


1 Answers

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"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >      <ListView         android:id="@+id/list"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_alignParentTop="true" >      </ListView>  </RelativeLayout> 

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" >      <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <LinearLayout             android:layout_width="255dp"             android:layout_height="wrap_content"             android:orientation="vertical" >              <LinearLayout                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:orientation="vertical" >                  <TextView                     android:id="@+id/title"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:text="Video1"                     android:textAppearance="?android:attr/textAppearanceLarge"                     android:textColor="#339966"                     android:textStyle="bold" />             </LinearLayout>              <LinearLayout                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:orientation="vertical" >                  <TextView                     android:id="@+id/detail"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:text="video1"                     android:textColor="#606060" />             </LinearLayout>         </LinearLayout>          <ImageView             android:id="@+id/img"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="@drawable/ic_launcher" />      </LinearLayout>  </LinearLayout> 

main.java:

package com.example.sample;  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 {      ListView l1;     String[] t1={"video1","video2"};     String[] d1={"lesson1","lesson2"};     int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         l1=(ListView)findViewById(R.id.list);         l1.setAdapter(new dataListAdapter(t1,d1,i1));     }      class dataListAdapter extends BaseAdapter {         String[] Title, Detail;         int[] imge;          dataListAdapter() {             Title = null;             Detail = null;             imge=null;         }          public dataListAdapter(String[] text, String[] text1,int[] text3) {             Title = text;             Detail = text1;             imge = text3;          }          public int getCount() {             // TODO Auto-generated method stub             return Title.length;         }          public Object getItem(int arg0) {             // TODO Auto-generated method stub             return null;         }          public long getItemId(int position) {             // TODO Auto-generated method stub             return position;         }          public View getView(int position, View convertView, ViewGroup parent) {              LayoutInflater inflater = getLayoutInflater();             View row;             row = inflater.inflate(R.layout.custom, parent, false);             TextView title, detail;             ImageView i1;             title = (TextView) row.findViewById(R.id.title);             detail = (TextView) row.findViewById(R.id.detail);             i1=(ImageView)row.findViewById(R.id.img);             title.setText(Title[position]);             detail.setText(Detail[position]);             i1.setImageResource(imge[position]);              return (row);         }     } } 

Try this.

like image 127
ishu Avatar answered Sep 19 '22 09:09

ishu