Advertisement
Guest User

CustomListAdapter.java

a guest
May 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. public class CustomListAdapter extends BaseAdapter {  
  2.     private Activity mContext;
  3.     private List<String> mListTitle;
  4.     private List<String> mListDescription;
  5.     private LayoutInflater mLayoutInflater = null;  
  6.  
  7.     public CustomListAdapter(Activity context, List<String> list) {  
  8.         mContext = context;  
  9.         mList = list;  
  10.         mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  11.     }
  12.  
  13.     @Override  
  14.     public int getCount() {  
  15.         return mListTitle.size();
  16.     }
  17.  
  18.     @Override  
  19.     public Object getItem(int pos) {  
  20.         return mListTitle.get(pos);
  21.     }
  22.  
  23.     @Override  
  24.     public long getItemId(int position) {  
  25.         return position;  
  26.     }
  27.      
  28.     @Override  
  29.     public View getView(int position, View convertView, ViewGroup parent) {  
  30.         View v = convertView;
  31.         CustomListViewHolder viewHolder;  
  32.        
  33.         if (convertView == null) {  
  34.             LayoutInflater li = (LayoutInflater) mContext  
  35.                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  36.             v = li.inflate(R.layout.customlistitem, null);  
  37.             viewHolder = new CustomListViewHolder(v);  
  38.             v.setTag(viewHolder);  
  39.         } else {  
  40.             viewHolder = (CustomListViewHolder) v.getTag();  
  41.         }
  42.        
  43.         // TODO: viewHolder.mIcon 에 그림 넣는 코드 추가
  44.         viewHolder.mTitle.setText(mListTitle.get(position));
  45.         viewHolder.mDesc.setText(mListDesc.get(position));
  46.         return v;
  47.     }
  48.    
  49.     //아이콘은 어떻게 구현하실지 몰라서 안했어요
  50.     public void addItem(String title, String description){
  51.         mListTitle.add(title);
  52.         mListDescription.add(description);
  53.         notifyDataSetChanged();
  54.     }
  55. }
  56.  
  57. class CustomListViewHolder {
  58.    
  59.     public ImageView mIcon;
  60.     public TextView mTitle;
  61.     public TextView mDesc;
  62.      
  63.     public CustomListViewHolder(View base) {  
  64.         mIcon = (TextView) base.findViewById(R.id.listitemicon);
  65.         mTitle = (TextView) base.findViewById(R.id.listitemtitle);
  66.         mDesc = (TextView) base.findViewById(R.id.listitemdesc);
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement