activity_main.xml
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" tools:context="iterator.ir.a015.MainActivity"> <ListView android:id="@+id/listViewMovies" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
listview_custom_row.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/black"> <ImageView android:layout_width="100dp" android:layout_height="75dp" app:srcCompat="@drawable/movie01" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/imageViewRow" android:paddingTop="5dp" android:paddingBottom="5dp"/> <TextView android:text="Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/imageViewRow" android:layout_toEndOf="@+id/imageViewRow" android:layout_marginLeft="15dp" android:layout_marginStart="15dp" android:maxWidth="150dp" android:id="@+id/textViewMovieTitle" android:gravity="center_vertical" android:textColor="@color/white"/> <TextView android:text="Rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/textViewRating" android:layout_marginRight="20dp" android:textColor="@color/white"/> <TextView android:text="Year" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/textViewRating" android:layout_toStartOf="@+id/textViewRating" android:id="@+id/textViewYear" android:layout_marginRight="5dp" android:textColor="@color/white"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#343236" android:layout_below="@id/imageViewRow"></View> </RelativeLayout>
MovieDataProvider.java
public class MovieDataProvider { public int Poster; public String Title; public String Year; public String Rating; public MovieDataProvider(int poster,String title,String year,String rating) { this.Poster=poster; this.Title=title; this.Year=year; this.Rating=rating; } }
MovieAdapter.java
public class MovieAdapter extends ArrayAdapter<MovieDataProvider> { public MovieAdapter(Context context, int resource) { super(context, resource); } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { View view=convertView; if (view==null) { LayoutInflater layoutInflater=LayoutInflater.from(getContext()); view=layoutInflater.inflate(R.layout.listview_custom_row,parent,false); } MovieDataProvider currentMovie=this.getItem(position); if (currentMovie!=null) { ImageView imageViewRow= (ImageView) view.findViewById(R.id.imageViewRow); TextView textViewMovieTitle= (TextView) view.findViewById(R.id.textViewMovieTitle); TextView textViewMovieYear= (TextView) view.findViewById(R.id.textViewYear); TextView textViewMovieRating= (TextView) view.findViewById(R.id.textViewRating); String year= String.format("(%s)",currentMovie.Year); imageViewRow.setImageResource(currentMovie.Poster); textViewMovieTitle.setText(currentMovie.Title); textViewMovieYear.setText(year); textViewMovieRating.setText(currentMovie.Rating); } return view; } }
MainActivity.java
public class MainActivity extends AppCompatActivity { ListView listViewMovies; MovieAdapter movieAdapter; int[] posters={ R.drawable.movie01, R.drawable.movie02, R.drawable.movie03, R.drawable.movie04, R.drawable.movie05, R.drawable.movie06, R.drawable.movie07, R.drawable.movie08, R.drawable.movie09, R.drawable.movie10 }; String[] titles; String[] years; String[] ratings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listViewMovies= (ListView) findViewById(R.id.listViewMovies); titles=getResources().getStringArray(R.array.movie_titles); years=getResources().getStringArray(R.array.movie_years); ratings=getResources().getStringArray(R.array.movie_ratings); movieAdapter=new MovieAdapter(getApplicationContext(),R.layout.listview_custom_row); for (int i=0;i<10;i++) { MovieDataProvider movieDataProvider=new MovieDataProvider(posters[i],titles[i], years[i],ratings[i]); movieAdapter.add(movieDataProvider); } listViewMovies.setAdapter(movieAdapter); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/015
http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view