/*
 * Copyright © 2015 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.android.widget.thai.address;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import th.or.nectec.android.widget.thai.R;
import th.or.nectec.entity.thai.AddressEntity;

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

class AddressAdapter <T extends AddressEntity> extends BaseAdapter {

    ArrayList<T> addressList;
    LayoutInflater mInflater;

    public AddressAdapter(Context context, List<T> addressList) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addAll(addressList);
    }


    public void addAll(List<T> collection) {
        addressList = new ArrayList<>();
        addressList.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return createViewFromResources(position, convertView, parent);
    }

    public View createViewFromResources(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.address_picker_list_item, parent, false);
        }

        TextView text = (TextView) convertView;

        AddressEntity entity = getItem(position);
        text.setText(entity.getName());
        return text;
    }

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

    @Override
    public T getItem(int position) {
        return addressList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResources(position, convertView, parent);
    }
}

@@ -13,9 +13,11 @@
 ght 
-%C2%A9
+(c)
  201
@@ -1060,17 +1060,16 @@
 sAdapter
- 
 %3CT exten
@@ -1113,21 +1113,16 @@
  %7B%0A%0A    
-Array
 List%3CT%3E 
@@ -1335,22 +1335,23 @@
         
-addAll
+setList
 (address
@@ -1368,31 +1368,32 @@
  %7D%0A%0A
-%0A
     p
-ublic void addAll
+rivate void setList
 (Lis
@@ -1644,39 +1644,23 @@
  return 
-createViewFromResources
+getView
 (positio
@@ -1695,408 +1695,8 @@
  %7D%0A%0A
-    public View createViewFromResources(int position, View convertView, ViewGroup parent) %7B%0A        if (convertView == null) %7B%0A            convertView = mInflater.inflate(R.layout.address_picker_list_item, parent, false);%0A        %7D%0A%0A        TextView text = (TextView) convertView;%0A%0A        AddressEntity entity = getItem(position);%0A        text.setText(entity.getName());%0A        return text;%0A    %7D%0A%0A
     
@@ -2065,69 +2065,258 @@
     
-return createViewFromResources(position, convertView, parent)
+if (convertView == null) %7B%0A            convertView = mInflater.inflate(R.layout.address_picker_list_item, parent, false);%0A        %7D%0A        TextView text = (TextView) convertView;%0A        text.setText(getItem(position).getName());%0A        return text
 ;%0A  

Refactor AddressAdapter