Answer the question
In order to leave comments, you need to log in
How to fill Spiner with data from Web service using tasks?
Task.
You need to get a list of users from the web service and transfer it to the spinner.
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) {
}
});
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question