티스토리 뷰

android

Custom AutoCompleteTextView

구름나드리 2018. 10. 23. 16:48

Custom AutoCompleteTextView









<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/actv"
android:completionThreshold="1"
android:hint="Select a country"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</LinearLayout>



public class MainActivity extends AppCompatActivity {

private List<CountryItem> countryList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillCountryList();

AutoCompleteTextView editText = findViewById(R.id.actv);
AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, countryList);
editText.setAdapter(adapter);

}

private void fillCountryList() {

countryList = new ArrayList<>();
countryList.add(new CountryItem("Afghanistan", R.drawable.screenshot_11));
countryList.add(new CountryItem("albanaia", R.drawable.screenshot_12));
countryList.add(new CountryItem("Algeria", R.drawable.screenshot_13));
countryList.add(new CountryItem("Korea", R.drawable.screenshot_11));

}


}




public class CountryItem {
private String countryName;
private int flagImage;

public CountryItem(String countryName, int flagImage) {
this.countryName = countryName;
this.flagImage = flagImage;
}

public String getCountryName() {
return countryName;
}

public int getFlagImage() {
return flagImage;
}

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp"
xmlns:tools="http://schemas.android.com/tools">

<ImageView
android:id="@+id/image_view_flag"
tools:src="@drawable/screenshot_12"
android:layout_width="60dp"
android:layout_height="40dp" />

<TextView
android:id="@+id/text_view_name"
android:layout_gravity="center_vertical"
android:paddingStart="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
tools:text="korea"/>

</LinearLayout>




class AutoCompleteAdapter extends ArrayAdapter<CountryItem> {

private List<CountryItem> countryListFull;

public AutoCompleteAdapter(@NonNull Context context, @NonNull List<CountryItem> countryList) {
super(context, 0, countryList);
countryListFull = new ArrayList<>(countryList);
}

@NonNull
@Override
public Filter getFilter() {
return countryFilter;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.country_autocomplete_row, parent, false
);
}

TextView textView = convertView.findViewById(R.id.text_view_name);
ImageView imageView = convertView.findViewById(R.id.image_view_flag);

CountryItem countryItem = getItem(position);

if (countryItem != null) {
textView.setText(countryItem.getCountryName());
imageView.setImageResource(countryItem.getFlagImage());
}
return convertView;
}

private Filter countryFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

FilterResults results = new FilterResults();
List<CountryItem> suggestions = new ArrayList<>();

if (constraint == null || constraint.length() == 0) {
suggestions.addAll(countryListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();

for (CountryItem item : countryListFull) {
if (item.getCountryName().toLowerCase().contains(filterPattern)) {
suggestions.add(item);
}
}
}

results.values = suggestions;
results.count = suggestions.size();

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List) results.values);
notifyDataSetChanged();
}

@Override
public CharSequence convertResultToString(Object resultValue) {
return ((CountryItem) resultValue).getCountryName();
}
};


}






'android' 카테고리의 다른 글

imeOptions 사용  (0) 2018.10.25
HashMap 사용방법  (0) 2018.10.25
spannable string  (0) 2018.10.23
Remove the ActionBar  (0) 2018.10.16
TextSwitcher 사용  (0) 2018.10.16
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함