Advertisement
Guest User

MyAdapter

a guest
Dec 26th, 2012
3,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package com.demo.nettest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.content.Context;
  7. import android.graphics.Color;
  8. import android.util.Log;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseAdapter;
  13. import android.widget.TextView;
  14.  
  15.  
  16. public class MyAdapter extends BaseAdapter{
  17.     private LayoutInflater myInflater;
  18.     private List<Movie> movies;
  19.    
  20.     public MyAdapter(Context context,List<Movie> movie){
  21.         myInflater = LayoutInflater.from(context);
  22.         this.movies = movie;
  23.     }
  24.     /*private view holder class*/
  25.     private class ViewHolder {
  26.         TextView txtTitle;
  27.         TextView txtTime;
  28.         public ViewHolder(TextView txtTitle, TextView txtTime){
  29.             this.txtTitle = txtTitle;
  30.             this.txtTime = txtTime;
  31.         }
  32.     }
  33.  
  34.     @Override
  35.     public int getCount() {
  36.         return movies.size();
  37.     }
  38.  
  39.     @Override
  40.     public Object getItem(int arg0) {
  41.         return movies.get(arg0);
  42.     }
  43.  
  44.     @Override
  45.     public long getItemId(int position) {
  46.         return movies.indexOf(getItem(position));
  47.     }
  48.  
  49.     @Override
  50.     public View getView(int position, View convertView, ViewGroup parent) {
  51.         ViewHolder holder = null;
  52.         if(convertView==null){
  53.             convertView = myInflater.inflate(R.layout.list_item, null);
  54.             holder = new ViewHolder(
  55.                     (TextView) convertView.findViewById(R.id.title),
  56.                     (TextView) convertView.findViewById(R.id.time)
  57.                     );
  58.             convertView.setTag(holder);        
  59.         }else{
  60.             holder = (ViewHolder) convertView.getTag();
  61.         }
  62.         Movie movie = (Movie)getItem(position);
  63.         //0 = movie, 1 = title, 2 = nine
  64.         int color_title[] = {Color.WHITE,Color.WHITE,Color.YELLOW};
  65.         int color_time[] = {Color.WHITE,Color.WHITE,Color.YELLOW};
  66.         int color_back[] = {Color.BLACK,Color.BLUE,Color.BLACK};
  67.         int time_vis[] = {View.VISIBLE,View.GONE,View.VISIBLE};
  68.        
  69.         int type_num = movie.getType();
  70.         holder.txtTitle.setText(movie.getName());
  71.         holder.txtTitle.setTextColor(color_title[type_num]);
  72.         holder.txtTitle.setBackgroundColor(color_back[type_num]);
  73.         holder.txtTime.setText(movie.getTime());
  74.         holder.txtTime.setTextColor(color_time[type_num]);         
  75.         holder.txtTime.setVisibility(time_vis[type_num]);
  76.  
  77.         return convertView;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement