android - Spinner won't show item selected through data in firebase -
when fetch data firebase can show list through spinner when item selected doesn't show anything, list, works if hardcode list string values. , onitemselectedlistener doesn't work, can please me?
here's how fetch data.
private arraylist <string> fetchdata (string league){ final arraylist<string> teamname = new arraylist<>(); /* return arraylist of type string of teams in league*/ databasereference databasereference = firebasedatabase.getinstance() .getreference() .child("standings").child(league); query query = databasereference.orderbychild("name"); query.addlistenerforsinglevalueevent(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { for(datasnapshot mdatasnapshot: datasnapshot.getchildren()){ string name = (string) mdatasnapshot.child("name").getvalue(); teamname.add(name); } } @override public void oncancelled(databaseerror databaseerror) { } }); return teamname; }
here oncreate method setup adapter:
arraylist<string> teamsofleague = fetchdata(league); arrayadapter<string> madapter = new arrayadapter<>( this,r.layout.spinner_layout, r.id.text1, teamsofleague); mlocalteam.setonitemselectedlistener(new spinner.onitemselectedlistener(){ public void onitemselected(adapterview<?> parent, view view, int pos, long id) { log.e(tag, "im being selected"); } public void onnothingselected(adapterview<?> parent) { } }); mlocalteam.setadapter(madapter);
here main layout activity:
<linearlayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <spinner android:id="@+id/spinner" android:backgroundtint="@color/coloraccent" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout>
here spinner.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:background="@color/colorprimarydark" android:layout_width="match_parent" android:textcolor="@color/white" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelistitemsmall" android:gravity="center_vertical" android:spinnermode="dropdown" android:paddingstart="?android:attr/listpreferreditempaddingstart" android:paddingend="?android:attr/listpreferreditempaddingend" android:minheight="?android:attr/listpreferreditemheightsmall" android:paddingleft="?android:attr/listpreferreditempaddingleft" android:paddingright="?android:attr/listpreferreditempaddingright" /> </linearlayout>
and here how activity when run app: screenshot
Comments
Post a Comment