public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spiner = findViewById(R.id.spiner);
AsyncTaskRunner task = new AsyncTaskRunner(spiner,getApplicationContext());
task.execute();
}
}
class ListUser {
@SerializedName("User")
@Expose
private String user;
@SerializedName("Uid")
@Expose
private String uid;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
// ...
// ...
class AsyncTaskRunner extends AsyncTask<Void, Void, List<ListUser>> {
Spinner spinner;
Context context;
public AsyncTaskRunner(Spinner spinner, Context context){
this.spinner = spinner;
this.context = context;
}
@Override
protected List<ListUser> doInBackground(Void... params) {
List<Header> headers = new ArrayList<>();
ApiService api = new Api(headers);
headers.add(new AuthHeader("http", "http"));
List<ListUser> users = api.getUsers();
return users;
}
@Override
protected void onPostExecute(List<ListUser> result) {
final List<ListUser> users = result;
ArrayAdapter<ListUser> adapter = new ArrayAdapter<>(context,R.layout.row,R.id.textRow, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context,users.get(position).getUser(),Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}