http://gacken.com/wp/program/android/1927/
I put the adapter in the Android fragment for reference.
1 2 3 is displayed, but no groups are displayed.
How do I resolve this?
ItemListFragment.java
public class ItemListFragment extensions ListFragment{
private Callbacks mCallbacks =sDummyCallbacks;
public classGroupSpinnerItem{
public boolean isGroup;// group flag
public String itemName;// Item name
publicGroupSpinnerItem(boolean isGroup, StringitemName){
This.isGroup = isGroup;
This.itemName =itemName;
}
}
public interface Callbacks {
void onItemSelected (String id);
}
private static CallbackssDummyCallbacks=newCallbacks(){
@ Override
public void onItemSelected (String id) {
}
};
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GroupSpinnerAdapter=newGroupSpinnerAdapter(getContext());
adapter.addItem(newGroupSpinnerItem(true, "group"));
adapter.addItem(newGroupSpinnerItem(false, "1"));
adapter.addItem(newGroupSpinnerItem(false, "2"));
adapter.addItem(newGroupSpinnerItem(false, "3"));
setListAdapter (adapter);
}
GroupSpinnerAdapter.java
public classGroupSpinnerAdapter extensions BaseAdapter {
public static ArrayList<ItemListFragment.GroupSpinnerItem>itemList=newArrayList<ItemListFragment.GroupSpinnerItem>();
public static LayoutInflater inflater;
publicGroupSpinnerAdapter(Contextcontext){
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static void addItem(ItemListFragment.GroupSpinnerItem){
itemList.add(item);
}
public intgetCount(){
return itemList.size();
}
publicObjectgetItem(int position){
return itemList.get(position);
}
public long getItemId(int position){
return position;
}
publicView getView(int position, View convertView, ViewGroup parent) {
return createView(position, convertView, parent, false);
}
@ Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return createView(position, convertView, parent, true);
}
publicView createView(int position, View convertView, ViewGroup parent, boolean isDropDown){
TextView tvName = null;
if(convertView instance of TextView) {
tvName=(TextView) convertView;
}
if(tvName==null){
tvName=(TextView) inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
}
ItemListFragment.GroupSpinnerItem=itemList.get(position);
if(item.isGroup){
if(isDropDown){
tvName.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
tvName.setText(item.itemName);
tvName.setBackgroundColor (Color.BLUE);
}
}
else{
String text="+item.itemName;
tvName.setText(text);
tvName.setBackgroundColor (Color.TRANSPARENT);
}
return tvName;
}
@ Override
public boolean isEnabled (int position) {
return(itemList.get(position).isGroup==false);
}
}
The reference page applies the adapter that inherited the BaseAdapter to Spinner.In some cases, this can be diverted directly to the adapter used in ListView (ListFragment), but this is not the case.
The last argument (isDropDown) in createView is always false because the adapter applied to ListView does not call getDropDownView.Therefore, "groups" are not displayed in ListView.
In order to solve this problem for now, I think you can change the last argument to true as below.
publicView getView(int position, View convertView, ViewGroup parent){
return createView(position, convertView, parent, true);
}
If you decide to use it for Spinner later, return it to false.
925 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
624 Uncaught (inpromise) Error on Electron: An object could not be cloned
585 PHP ssh2_scp_send fails to send files as intended
574 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.