2626
2727import android .content .res .Resources ;
2828import android .content .Context ;
29+ import android .os .Handler ;
30+ import android .os .Looper ;
31+
2932import android .util .Log ;
3033import android .view .View ;
3134import android .view .ViewGroup ;
@@ -43,6 +46,7 @@ private static class ViewHolder {
4346 }
4447
4548 // Data to be shown with the adapter
49+ private ArrayList <String > dataStr ;
4650 private ArrayList <NetworkInterfaceTester .NetIfData > data ;
4751 private int dataSize ;
4852
@@ -52,6 +56,9 @@ private static class ViewHolder {
5256 private LayoutInflater mInflater ;
5357
5458
59+ // UI related
60+ private Handler handler ;
61+
5562
5663
5764 public ListenIfAdapter (NetworkInterfaceTester nit , Context context ) {
@@ -60,8 +67,10 @@ public ListenIfAdapter(NetworkInterfaceTester nit, Context context) {
6067 this .mContext = context ;
6168 this .mInflater = LayoutInflater .from (this .mContext );
6269
70+ this .handler = new Handler (Looper .getMainLooper ());
71+
6372 nit .addOnNetworkStateChangedListener (this );
64- this .onNetworkStateChanged (nit , null , false );
73+ this .onNetworkStateChanged (nit );
6574 }
6675
6776
@@ -80,9 +89,19 @@ public int getItemPositionByIfName(String ifName) {
8089
8190
8291
92+ @ Override
93+ public View getDropDownView (int position , View convertView , ViewGroup parent ) {
94+ return this .handleViewRecreation (position , convertView , parent );
95+ }
96+
8397
8498 @ Override
8599 public View getView (int position , View convertView , ViewGroup parent ) {
100+ return this .handleViewRecreation (position , convertView , parent );
101+ }
102+
103+
104+ private View handleViewRecreation (int position , View convertView , ViewGroup parent ) {
86105 if (convertView == null ) { // Check if view must be recreated using the famous ViewHolder pattern
87106 convertView = this .mInflater .inflate (R .layout .spinner_row , parent , false );
88107
@@ -92,13 +111,14 @@ public View getView(int position, View convertView, ViewGroup parent) {
92111 }
93112
94113 ViewHolder vh = (ViewHolder )convertView .getTag ();
95- NetworkInterfaceTester . NetIfData nid = this .getItem (position );
96- vh .txtLabel .setText (nid . toString () );
114+ String label = this .dataStr . get (position );
115+ vh .txtLabel .setText (label );
97116
98117 return convertView ;
99118 }
100119
101120
121+
102122 @ Override
103123 public NetworkInterfaceTester .NetIfData getItem (int position ) {
104124 if (0 <= position && position < this .getCount ()) {
@@ -108,15 +128,35 @@ public NetworkInterfaceTester.NetIfData getItem(int position) {
108128 }
109129
110130
131+
111132 @ Override
112133 public int getCount () {
113134 return this .dataSize ;
114135 }
115136
116137
117138
118- public void onNetworkStateChanged (NetworkInterfaceTester nit , NetworkInterface iface , boolean enabled ) {
139+ public void onNetworkStateChanged (NetworkInterfaceTester nit ) {
119140 this .data = nit .getAvailableInterfaces ();
120141 this .dataSize = this .data .size ();
142+
143+ this .dataStr = new ArrayList <>();
144+ for (NetworkInterfaceTester .NetIfData nid : this .data ) {
145+ String actualName = " (" + nid .getName () + ")" ;
146+ String displayName = nid .getDisplayName ();
147+
148+ if (nid .getName ().equals ("0.0.0.0" )) {
149+ displayName = this .mContext .getResources ().getString (R .string .main_activity_settings_listenif_spin_any );
150+ }
151+
152+ this .dataStr .add (displayName + actualName );
153+ }
154+
155+ // update Spinner
156+ this .handler .post (new Runnable () {
157+ public void run () {
158+ ListenIfAdapter .this .notifyDataSetChanged ();
159+ }
160+ });
121161 }
122162}
0 commit comments