custom-list-adapter

 

Custom List Adapter is one of the things you have to create in most of the application as the pre-loaded list templates are not able to handle custom list data.

Steps to create a custom list adapter:

Create the model for your data.

Lyric.java

public class Lyric {
int thumb;
String lyricTitle;
String lyricArtist;

// default Constructor
public Lyric(){}

// constructor
public Lyric(int thumb, String lyricTitle, String lyricArtist){
this.thumb = thumb;
this.lyricTitle = lyricTitle;
this.lyricArtist = lyricArtist;
}

public int getThumb() {
return thumb;
}

public void setThumb(int thumb) {
this.thumb = thumb;
}

public String getLyricTitle() {
return lyricTitle;
}

public void setLyricTitle(String lyricTitle) {
this.lyricTitle = lyricTitle;
}

public String getLyricArtist() {
return lyricArtist;
}

public void setLyricArtist(String lyricArtist) {
this.lyricArtist = lyricArtist;
}
}

 

Create the template for a row item.

row_lyric.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imgThumb"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>

<TextView
android:id="@+id/lblTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Love The Way You Lie"

android:layout_marginTop="25dp"
android:layout_marginLeft="20dp"

android:textStyle="bold"
android:textSize="20dp"
android:layout_toRightOf="@id/imgThumb"/>

<TextView
android:id="@+id/lblArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eminem ft. Rihanna"

android:layout_marginLeft="20dp"

android:textStyle="bold"
android:layout_toRightOf="@id/imgThumb"
android:layout_below="@id/lblTitle"/>

</RelativeLayout>

Now create adapter
LyricListAdapter

public class LyricListAdapter extends BaseAdapter {

Context context;
List<Lyric> lyrics;

public LyricListAdapter(Context context, List<Lyric> lyrics){
this.context = context;
this.lyrics = lyrics;
}

@Override
public int getCount() {
return lyrics.size();
}

@Override
public Object getItem(int position) {
return lyrics.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

//View holder
class ViewHolder{
ImageView imgThumb;
TextView lblTitle;
TextView lblArtist;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

if(convertView == null){
convertView = inflater.inflate(R.layout.row_lyric, null);

holder = new ViewHolder();
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.lblTitle = (TextView) convertView.findViewById(R.id.lblTitle);
holder.lblArtist = (TextView) convertView.findViewById(R.id.lblArtist);

convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}

try{
Lyric lyric = (Lyric) getItem(position);

holder.imgThumb.setImageResource(lyric.getThumb());
holder.lblTitle.setText(lyric.getLyricTitle());
holder.lblArtist.setText(lyric.getLyricArtist());
}catch (Exception ex){
ex.printStackTrace();
}

return convertView;
}
}

Open xml for main activity
activity_main.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

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

</LinearLayout>

Open main activity

MainActivity.java

public class LyricsListActivity extends AppCompatActivity {

int[] thumbnails = {
R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,
};

String[] lyricTitles = {
"Love The Way You Lie", "Monster", "Umbrella", "Beautiful Now", "Wiggle",
"Ray Bands", "One Love", "Criminal", "No Promises", "Replay"
};
String[] lyricArtists = {
"Eminem ft. Rihanna", "Eminem ft. Rihanna", "Rihanna", "Zedd", "Jason Derulo",
"B.O.B.", "Blue", "Britney Spears", "Shayne Ward", "Iyaz"
};

List<Lyric> lyrics;
ListView lyricsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lyrics_list);

lyrics = new ArrayList<Lyric>();

for(int i=0;i<10;i++){
Lyric lyricObject = new Lyric();

lyricObject.setThumb(thumbnails[i]);
lyricObject.setLyricTitle(lyricTitles[i]);
lyricObject.setLyricArtist(lyricArtists[i]);

lyrics.add(lyricObject);
}

lyricsList = (ListView) findViewById(R.id.lyricsList);
LyricListAdapter adapter = new LyricListAdapter(this, lyrics);
lyricsList.setAdapter(adapter);

}

}
Advertisements