1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.databinding.testapp.adapter; 17 18import android.databinding.BindingAdapter; 19import android.databinding.testapp.GenericView; 20import android.widget.TextView; 21 22import java.util.ArrayList; 23import java.util.Arrays; 24import java.util.Collection; 25import java.util.List; 26 27public class GenericAdapter { 28 29 @BindingAdapter("textList1") 30 public static <T> void setListText(TextView view, List<T> list) { 31 setText(view, list); 32 } 33 34 @BindingAdapter("textList2") 35 public static <T> void setCollectionText(TextView view, Collection<T> list) { 36 setText(view, list); 37 } 38 39 @BindingAdapter("textArray") 40 public static <T> void setArrayText(TextView view, T[] values) { 41 setText(view, Arrays.asList(values)); 42 } 43 44 @BindingAdapter({"textList1", "textArray"}) 45 public static <T> void setListAndArray(TextView view, List<T> list, T[] values) { 46 setText(view, list); 47 } 48 49 @BindingAdapter("list") 50 public static <T> void setGenericViewValue(GenericView<T> view, List<T> value) { 51 view.setList(value); 52 } 53 54 @BindingAdapter({"list", "array"}) 55 public static <T> void setGenericListAndArray(GenericView<T> view, List<T> list, T[] values) { 56 view.setList(list); 57 } 58 59 @BindingAdapter("textList3") 60 public static void setGenericList(TextView view, List<String> list) { 61 setText(view, list); 62 } 63 64 @BindingAdapter("textList3") 65 public static void setGenericIntegerList(TextView view, List<Integer> list) { 66 } 67 68 private static <T> void setText(TextView view, Collection<T> collection) { 69 StringBuilder stringBuilder = new StringBuilder(); 70 boolean isFirst = true; 71 for (T val : collection) { 72 if (isFirst) { 73 isFirst = false; 74 } else { 75 stringBuilder.append(' '); 76 } 77 stringBuilder.append(val.toString()); 78 } 79 view.setText(stringBuilder.toString()); 80 } 81} 82