ListView with Custom Adapter

ListView with Custom Adapter Example in Android

MainCLASS

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

ListView listView;
int[] movie_poster_resource = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10};
String[] movie_titles;
String[] movie_ratings;
MovieAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_View);
movie_ratings = getResources().getStringArray(R.array.movie_rating);
movie_titles = getResources().getStringArray(R.array.movie_title);
int i = 0;
adapter = new MovieAdapter(getApplicationContext(), R.layout.raw_layout);
listView.setAdapter(adapter);
for(String titles : movie_titles){
MovieDataProvider dataProvider = new MovieDataProvider(movie_poster_resource[i], titles, movie_ratings[i]);
adapter.add(dataProvider);
i++;
}

}
}

MainXML

<?xml version=”1.0″ encoding=”utf-8″?>
<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=”com.example.malik.test.MainActivity”
android:background=”#000000″
>
<ListView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/list_View”
/>
</RelativeLayout>

 

MovieAdapter CLASS

package com.example.malik.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* Created by malik on 09/06/2016.
*/
public class MovieAdapter extends ArrayAdapter {

List list = new ArrayList();

public MovieAdapter(Context context, int resource) {
super(context, resource);
}

static class DataHandler {

ImageView poster;
TextView title;
TextView rating;

}

@Override
public void add(Object object) {
super.add(object);
list.add(object);
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.raw_layout, parent, false);

handler = new DataHandler();
handler.poster = (ImageView) row.findViewById(R.id.movie_poster);
handler.title = (TextView) row.findViewById(R.id.movie_title);
handler.rating = (TextView) row.findViewById(R.id.movie_rating);
row.setTag(handler);
} else {
handler = (DataHandler) row.getTag();
}

MovieDataProvider dataProvider;
dataProvider = (MovieDataProvider) this.getItem(position);
handler.poster.setImageResource(dataProvider.getMovie_poster_resource());
handler.title.setText(dataProvider.getMovie_title());
handler.rating.setText(dataProvider.getMovie_rating());
return row;
}
}

 

MovieDataProvider CLASS

package com.example.malik.test;

/**
* Created by malik on 09/06/2016.
*/
public class MovieDataProvider {

private int movie_poster_resource;
private String movie_title;
private String movie_rating;

public MovieDataProvider(int movie_poster_resource, String movie_title, String movie_rating){
this.setMovie_poster_resource(movie_poster_resource);
this.setMovie_rating(movie_rating);
this.setMovie_title(movie_title);
}

public int getMovie_poster_resource() {
return movie_poster_resource;
}

public void setMovie_poster_resource(int movie_poster_resource) {
this.movie_poster_resource = movie_poster_resource;
}

public String getMovie_title() {
return movie_title;
}

public void setMovie_title(String movie_title) {
this.movie_title = movie_title;
}

public String getMovie_rating() {
return movie_rating;
}

public void setMovie_rating(String movie_rating) {
this.movie_rating = movie_rating;
}
}

 

raw_layout XML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical” android:layout_width=”match_parent”
android:layout_height=”80dp”
android:background=”#000000″
>

<ImageView
android:layout_width=”100dp”
android:layout_height=”75dp”
android:layout_alignParentLeft=”true”
android:id=”@+id/movie_poster”
android:src=”@drawable/pic1″
/>

<TextView
android:layout_width=”175dp”
android:layout_height=”75dp”
android:id=”@+id/movie_title”
android:layout_toRightOf=”@+id/movie_poster”
android:text=”This is movie name”
android:gravity=”center”
android:textColor=”#ffffff”
/>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”75dp”
android:layout_toRightOf=”@+id/movie_title”
android:layout_alignParentRight=”true”
android:text=”This is Rating”
android:gravity=”center”
android:textColor=”#ffffff”
android:id=”@+id/movie_rating”
/>

<View
android:layout_width=”match_parent”
android:layout_height=”2dp”
android:background=”#ffffff”
android:layout_below=”@+id/movie_poster”
></View>

</RelativeLayout>

 

string xml Inside Value Folder

<resources>
<string name=”app_name”>Test</string>
<string name=”action_settings”>Settings</string>
<string-array name=”movie_title”>
<item>Predentation</item>
<item>The Dark Night</item>
<item>The Notebook</item>
<item>American Sniper</item>
<item>Bird <Man></Man></item>
<item>The Imitation Game</item>
<item>Interlation</item>
<item>Selma <Man></Man></item>
<item>Fox</item>
<item>Fury</item>
</string-array>

<string-array name=”movie_rating”>
<item> Rating 7.1/10 </item>
<item> Rating 7.9/10 </item>
<item> Rating 9/10 </item>
<item> Rating 8.5/10 </item>
<item> Rating 3.9/10 </item>
<item> Rating 5.9/10 </item>
<item> Rating 7.1/10 </item>
<item> Rating 2.5/10 </item>
<item> Rating 9.5/10 </item>
<item> Rating 7.1/10 </item>
</string-array>

</resources>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.