OnItemSelectedListener not responding for Spinner in Android
up vote
0
down vote
favorite
Firstly I have 3 spinners(ConverterNo., ZoneNo., CodeID). So, populating ConverterNo. first and based on the values from 1st spinner 2nd spinner (ZoneNo.) should be populated and so on. Therefore, I used onItemSelectedListener
until so far. But it isn't helping or I am lost somewhere in the middle.
This is the code leading to populate the Spinners.
ERROR: method getServerResponse2 in class ListViewReqDao cannot
be applied to given types; required: no arguments found: int reason:
actual and formal argument lists differ in length
Note: The location of the error is in selectcode.java class inside the method getData1()
on line List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
selectcode.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class selectcode extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listView;
ListViewReqDao listViewReqDao;
Request requestdata;
ArrayList<String> namess;
ArrayList<String> meth;
ArrayList<String> Three;
String Name;
Spinner list_item,two,three;
List<Request> requests;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
list_item = findViewById(R.id.selectconverter);
two = findViewById(R.id.selectzone);
three = findViewById(R.id.selectcode);
getData();
getData1();
getData2();
}
private void getData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
final String items = new String[listViewReqDao.getServerResponse1().size()];
for (int i = 0; i < listViewReqDao.getServerResponse1().size(); i++) {
items[i] = listViewReqDao.getServerResponse1().get(i).getConverternumber();
}
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list_item.setAdapter(dataAdapter);
list_item.getSelectedItemPosition();
}
//Creating an String array for the ListView
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getData1() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata1();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
**// ERROR OCCURS IN THE BELOW LINE**
List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
final String items1 = new String[listViewReqDao.getServerResponse2().size()];
for (int j = 0; j < listViewReqDao.getServerResponse2().size(); j++) {
items1[j] = listViewReqDao.getServerResponse2().get(j).getZonenumber();
}
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
two.setAdapter(dataAdapter1);
// dataAdapter1.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
private void getData2(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata2();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
String items2 = new String[listViewReqDao.getServerResponse3().size()];
for (int k = 0; k < listViewReqDao.getServerResponse3().size(); k++) {
items2[k] = listViewReqDao.getServerResponse3().get(k).getCodeid();
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
three.setAdapter(dataAdapter2);
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// String sSelected = parent.getItemAtPosition(position).toString();
// Toast.makeText(this,sSelected,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
ListViewReqDao.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ListViewReqDao {
@SerializedName("server_response1")
@Expose
private List<Request> serverResponse1 = null;
@SerializedName("server_response2")
@Expose
private List<Request> serverResponse2 = null;
@SerializedName("server_response3")
@Expose
private List<Request> serverResponse3 = null;
public List<Request> getServerResponse1() {
return serverResponse1;
}
public void setServerResponse1(List<Request> serverResponse1) {
this.serverResponse1 = serverResponse1;
}
public List<Request> getServerResponse2() {
return serverResponse2;
}
public void setServerResponse2(List<Request> serverResponse2) {
this.serverResponse2 = serverResponse2;
}
public List<Request> getServerResponse3() {
return serverResponse3;
}
public void setServerResponse3(List<Request> serverResponse3) {
this.serverResponse3 = serverResponse3;
}
}
Request.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Request {
@SerializedName("converternumber")
@Expose
private String converternumber;
@SerializedName("zonenumber")
@Expose
private String zonenumber;
@SerializedName("codeid")
@Expose
private String codeid;
public String getConverternumber() {
return converternumber;
}
public void setConverternumber(String converternumber) {
this.converternumber = converternumber;
}
public String getZonenumber() {
return zonenumber;
}
public void setZonenumber(String zonenumber) {
this.zonenumber = zonenumber;
}
public String getCodeid() {
return codeid;
}
public void setCodeid(String codeid) {
this.codeid = codeid;
}
}
Api.java
package com.example.app;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "http://......url...../app1/";
@GET("getconverterjson.php")
Call<ListViewReqDao> getdata();
@GET("getzonejson.php")
Call<ListViewReqDao> getdata1();
@GET("getcodejson.php")
Call<ListViewReqDao> getdata2();
}
One of the JSON file among three Json file:
getzonejson.php (This is for loading 2nd spinner)
<?php
$db_name="app1";
$mysql_user="root";
$server_name="localhost";
// require "connection.php";
$connectionn=mysqli_connect($server_name,$mysql_user,"",$db_name);
// if(isset($_GET['converternumber']) && isset($_POST['zonenumber']) && isset($_POST['codeid']))
// {
$n_converternumber= 'converternumber';
$n_zonenumber= ['zonenumber'];
$sql_query= "select zonenumber from addcode where converternumber like '$n_converternumber';";
$result= mysqli_query($connectionn,$sql_query);
$response= array();
// print_r($result);
while($roww=mysqli_fetch_array($result)){
array_push($response,array("zonenumber"=>$roww[0]));
}
echo json_encode(array("server_response2"=>$response));
mysqli_close($connectionn);
?>
android retrofit android-arrayadapter android-spinner onitemselectedlistener
add a comment |
up vote
0
down vote
favorite
Firstly I have 3 spinners(ConverterNo., ZoneNo., CodeID). So, populating ConverterNo. first and based on the values from 1st spinner 2nd spinner (ZoneNo.) should be populated and so on. Therefore, I used onItemSelectedListener
until so far. But it isn't helping or I am lost somewhere in the middle.
This is the code leading to populate the Spinners.
ERROR: method getServerResponse2 in class ListViewReqDao cannot
be applied to given types; required: no arguments found: int reason:
actual and formal argument lists differ in length
Note: The location of the error is in selectcode.java class inside the method getData1()
on line List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
selectcode.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class selectcode extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listView;
ListViewReqDao listViewReqDao;
Request requestdata;
ArrayList<String> namess;
ArrayList<String> meth;
ArrayList<String> Three;
String Name;
Spinner list_item,two,three;
List<Request> requests;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
list_item = findViewById(R.id.selectconverter);
two = findViewById(R.id.selectzone);
three = findViewById(R.id.selectcode);
getData();
getData1();
getData2();
}
private void getData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
final String items = new String[listViewReqDao.getServerResponse1().size()];
for (int i = 0; i < listViewReqDao.getServerResponse1().size(); i++) {
items[i] = listViewReqDao.getServerResponse1().get(i).getConverternumber();
}
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list_item.setAdapter(dataAdapter);
list_item.getSelectedItemPosition();
}
//Creating an String array for the ListView
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getData1() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata1();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
**// ERROR OCCURS IN THE BELOW LINE**
List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
final String items1 = new String[listViewReqDao.getServerResponse2().size()];
for (int j = 0; j < listViewReqDao.getServerResponse2().size(); j++) {
items1[j] = listViewReqDao.getServerResponse2().get(j).getZonenumber();
}
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
two.setAdapter(dataAdapter1);
// dataAdapter1.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
private void getData2(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata2();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
String items2 = new String[listViewReqDao.getServerResponse3().size()];
for (int k = 0; k < listViewReqDao.getServerResponse3().size(); k++) {
items2[k] = listViewReqDao.getServerResponse3().get(k).getCodeid();
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
three.setAdapter(dataAdapter2);
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// String sSelected = parent.getItemAtPosition(position).toString();
// Toast.makeText(this,sSelected,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
ListViewReqDao.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ListViewReqDao {
@SerializedName("server_response1")
@Expose
private List<Request> serverResponse1 = null;
@SerializedName("server_response2")
@Expose
private List<Request> serverResponse2 = null;
@SerializedName("server_response3")
@Expose
private List<Request> serverResponse3 = null;
public List<Request> getServerResponse1() {
return serverResponse1;
}
public void setServerResponse1(List<Request> serverResponse1) {
this.serverResponse1 = serverResponse1;
}
public List<Request> getServerResponse2() {
return serverResponse2;
}
public void setServerResponse2(List<Request> serverResponse2) {
this.serverResponse2 = serverResponse2;
}
public List<Request> getServerResponse3() {
return serverResponse3;
}
public void setServerResponse3(List<Request> serverResponse3) {
this.serverResponse3 = serverResponse3;
}
}
Request.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Request {
@SerializedName("converternumber")
@Expose
private String converternumber;
@SerializedName("zonenumber")
@Expose
private String zonenumber;
@SerializedName("codeid")
@Expose
private String codeid;
public String getConverternumber() {
return converternumber;
}
public void setConverternumber(String converternumber) {
this.converternumber = converternumber;
}
public String getZonenumber() {
return zonenumber;
}
public void setZonenumber(String zonenumber) {
this.zonenumber = zonenumber;
}
public String getCodeid() {
return codeid;
}
public void setCodeid(String codeid) {
this.codeid = codeid;
}
}
Api.java
package com.example.app;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "http://......url...../app1/";
@GET("getconverterjson.php")
Call<ListViewReqDao> getdata();
@GET("getzonejson.php")
Call<ListViewReqDao> getdata1();
@GET("getcodejson.php")
Call<ListViewReqDao> getdata2();
}
One of the JSON file among three Json file:
getzonejson.php (This is for loading 2nd spinner)
<?php
$db_name="app1";
$mysql_user="root";
$server_name="localhost";
// require "connection.php";
$connectionn=mysqli_connect($server_name,$mysql_user,"",$db_name);
// if(isset($_GET['converternumber']) && isset($_POST['zonenumber']) && isset($_POST['codeid']))
// {
$n_converternumber= 'converternumber';
$n_zonenumber= ['zonenumber'];
$sql_query= "select zonenumber from addcode where converternumber like '$n_converternumber';";
$result= mysqli_query($connectionn,$sql_query);
$response= array();
// print_r($result);
while($roww=mysqli_fetch_array($result)){
array_push($response,array("zonenumber"=>$roww[0]));
}
echo json_encode(array("server_response2"=>$response));
mysqli_close($connectionn);
?>
android retrofit android-arrayadapter android-spinner onitemselectedlistener
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Firstly I have 3 spinners(ConverterNo., ZoneNo., CodeID). So, populating ConverterNo. first and based on the values from 1st spinner 2nd spinner (ZoneNo.) should be populated and so on. Therefore, I used onItemSelectedListener
until so far. But it isn't helping or I am lost somewhere in the middle.
This is the code leading to populate the Spinners.
ERROR: method getServerResponse2 in class ListViewReqDao cannot
be applied to given types; required: no arguments found: int reason:
actual and formal argument lists differ in length
Note: The location of the error is in selectcode.java class inside the method getData1()
on line List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
selectcode.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class selectcode extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listView;
ListViewReqDao listViewReqDao;
Request requestdata;
ArrayList<String> namess;
ArrayList<String> meth;
ArrayList<String> Three;
String Name;
Spinner list_item,two,three;
List<Request> requests;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
list_item = findViewById(R.id.selectconverter);
two = findViewById(R.id.selectzone);
three = findViewById(R.id.selectcode);
getData();
getData1();
getData2();
}
private void getData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
final String items = new String[listViewReqDao.getServerResponse1().size()];
for (int i = 0; i < listViewReqDao.getServerResponse1().size(); i++) {
items[i] = listViewReqDao.getServerResponse1().get(i).getConverternumber();
}
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list_item.setAdapter(dataAdapter);
list_item.getSelectedItemPosition();
}
//Creating an String array for the ListView
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getData1() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata1();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
**// ERROR OCCURS IN THE BELOW LINE**
List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
final String items1 = new String[listViewReqDao.getServerResponse2().size()];
for (int j = 0; j < listViewReqDao.getServerResponse2().size(); j++) {
items1[j] = listViewReqDao.getServerResponse2().get(j).getZonenumber();
}
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
two.setAdapter(dataAdapter1);
// dataAdapter1.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
private void getData2(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata2();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
String items2 = new String[listViewReqDao.getServerResponse3().size()];
for (int k = 0; k < listViewReqDao.getServerResponse3().size(); k++) {
items2[k] = listViewReqDao.getServerResponse3().get(k).getCodeid();
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
three.setAdapter(dataAdapter2);
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// String sSelected = parent.getItemAtPosition(position).toString();
// Toast.makeText(this,sSelected,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
ListViewReqDao.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ListViewReqDao {
@SerializedName("server_response1")
@Expose
private List<Request> serverResponse1 = null;
@SerializedName("server_response2")
@Expose
private List<Request> serverResponse2 = null;
@SerializedName("server_response3")
@Expose
private List<Request> serverResponse3 = null;
public List<Request> getServerResponse1() {
return serverResponse1;
}
public void setServerResponse1(List<Request> serverResponse1) {
this.serverResponse1 = serverResponse1;
}
public List<Request> getServerResponse2() {
return serverResponse2;
}
public void setServerResponse2(List<Request> serverResponse2) {
this.serverResponse2 = serverResponse2;
}
public List<Request> getServerResponse3() {
return serverResponse3;
}
public void setServerResponse3(List<Request> serverResponse3) {
this.serverResponse3 = serverResponse3;
}
}
Request.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Request {
@SerializedName("converternumber")
@Expose
private String converternumber;
@SerializedName("zonenumber")
@Expose
private String zonenumber;
@SerializedName("codeid")
@Expose
private String codeid;
public String getConverternumber() {
return converternumber;
}
public void setConverternumber(String converternumber) {
this.converternumber = converternumber;
}
public String getZonenumber() {
return zonenumber;
}
public void setZonenumber(String zonenumber) {
this.zonenumber = zonenumber;
}
public String getCodeid() {
return codeid;
}
public void setCodeid(String codeid) {
this.codeid = codeid;
}
}
Api.java
package com.example.app;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "http://......url...../app1/";
@GET("getconverterjson.php")
Call<ListViewReqDao> getdata();
@GET("getzonejson.php")
Call<ListViewReqDao> getdata1();
@GET("getcodejson.php")
Call<ListViewReqDao> getdata2();
}
One of the JSON file among three Json file:
getzonejson.php (This is for loading 2nd spinner)
<?php
$db_name="app1";
$mysql_user="root";
$server_name="localhost";
// require "connection.php";
$connectionn=mysqli_connect($server_name,$mysql_user,"",$db_name);
// if(isset($_GET['converternumber']) && isset($_POST['zonenumber']) && isset($_POST['codeid']))
// {
$n_converternumber= 'converternumber';
$n_zonenumber= ['zonenumber'];
$sql_query= "select zonenumber from addcode where converternumber like '$n_converternumber';";
$result= mysqli_query($connectionn,$sql_query);
$response= array();
// print_r($result);
while($roww=mysqli_fetch_array($result)){
array_push($response,array("zonenumber"=>$roww[0]));
}
echo json_encode(array("server_response2"=>$response));
mysqli_close($connectionn);
?>
android retrofit android-arrayadapter android-spinner onitemselectedlistener
Firstly I have 3 spinners(ConverterNo., ZoneNo., CodeID). So, populating ConverterNo. first and based on the values from 1st spinner 2nd spinner (ZoneNo.) should be populated and so on. Therefore, I used onItemSelectedListener
until so far. But it isn't helping or I am lost somewhere in the middle.
This is the code leading to populate the Spinners.
ERROR: method getServerResponse2 in class ListViewReqDao cannot
be applied to given types; required: no arguments found: int reason:
actual and formal argument lists differ in length
Note: The location of the error is in selectcode.java class inside the method getData1()
on line List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
selectcode.java
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class selectcode extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listView;
ListViewReqDao listViewReqDao;
Request requestdata;
ArrayList<String> namess;
ArrayList<String> meth;
ArrayList<String> Three;
String Name;
Spinner list_item,two,three;
List<Request> requests;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
list_item = findViewById(R.id.selectconverter);
two = findViewById(R.id.selectzone);
three = findViewById(R.id.selectcode);
getData();
getData1();
getData2();
}
private void getData() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
final String items = new String[listViewReqDao.getServerResponse1().size()];
for (int i = 0; i < listViewReqDao.getServerResponse1().size(); i++) {
items[i] = listViewReqDao.getServerResponse1().get(i).getConverternumber();
}
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
list_item.setAdapter(dataAdapter);
list_item.getSelectedItemPosition();
}
//Creating an String array for the ListView
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getData1() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata1();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
**// ERROR OCCURS IN THE BELOW LINE**
List<Request> list1 = listViewReqDao.getServerResponse2(list_item.getSelectedItemPosition());
final String items1 = new String[listViewReqDao.getServerResponse2().size()];
for (int j = 0; j < listViewReqDao.getServerResponse2().size(); j++) {
items1[j] = listViewReqDao.getServerResponse2().get(j).getZonenumber();
}
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
two.setAdapter(dataAdapter1);
// dataAdapter1.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
private void getData2(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<ListViewReqDao> call = api.getdata2();
call.enqueue(new Callback<ListViewReqDao>() {
@Override
public void onResponse(Call<ListViewReqDao> call, Response<ListViewReqDao> response) {
if (response.isSuccessful()) {
listViewReqDao = response.body();
String items2 = new String[listViewReqDao.getServerResponse3().size()];
for (int k = 0; k < listViewReqDao.getServerResponse3().size(); k++) {
items2[k] = listViewReqDao.getServerResponse3().get(k).getCodeid();
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, items2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
three.setAdapter(dataAdapter2);
}
}
@Override
public void onFailure(Call<ListViewReqDao> call, Throwable t) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// String sSelected = parent.getItemAtPosition(position).toString();
// Toast.makeText(this,sSelected,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
ListViewReqDao.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ListViewReqDao {
@SerializedName("server_response1")
@Expose
private List<Request> serverResponse1 = null;
@SerializedName("server_response2")
@Expose
private List<Request> serverResponse2 = null;
@SerializedName("server_response3")
@Expose
private List<Request> serverResponse3 = null;
public List<Request> getServerResponse1() {
return serverResponse1;
}
public void setServerResponse1(List<Request> serverResponse1) {
this.serverResponse1 = serverResponse1;
}
public List<Request> getServerResponse2() {
return serverResponse2;
}
public void setServerResponse2(List<Request> serverResponse2) {
this.serverResponse2 = serverResponse2;
}
public List<Request> getServerResponse3() {
return serverResponse3;
}
public void setServerResponse3(List<Request> serverResponse3) {
this.serverResponse3 = serverResponse3;
}
}
Request.java
package com.example.app;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Request {
@SerializedName("converternumber")
@Expose
private String converternumber;
@SerializedName("zonenumber")
@Expose
private String zonenumber;
@SerializedName("codeid")
@Expose
private String codeid;
public String getConverternumber() {
return converternumber;
}
public void setConverternumber(String converternumber) {
this.converternumber = converternumber;
}
public String getZonenumber() {
return zonenumber;
}
public void setZonenumber(String zonenumber) {
this.zonenumber = zonenumber;
}
public String getCodeid() {
return codeid;
}
public void setCodeid(String codeid) {
this.codeid = codeid;
}
}
Api.java
package com.example.app;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "http://......url...../app1/";
@GET("getconverterjson.php")
Call<ListViewReqDao> getdata();
@GET("getzonejson.php")
Call<ListViewReqDao> getdata1();
@GET("getcodejson.php")
Call<ListViewReqDao> getdata2();
}
One of the JSON file among three Json file:
getzonejson.php (This is for loading 2nd spinner)
<?php
$db_name="app1";
$mysql_user="root";
$server_name="localhost";
// require "connection.php";
$connectionn=mysqli_connect($server_name,$mysql_user,"",$db_name);
// if(isset($_GET['converternumber']) && isset($_POST['zonenumber']) && isset($_POST['codeid']))
// {
$n_converternumber= 'converternumber';
$n_zonenumber= ['zonenumber'];
$sql_query= "select zonenumber from addcode where converternumber like '$n_converternumber';";
$result= mysqli_query($connectionn,$sql_query);
$response= array();
// print_r($result);
while($roww=mysqli_fetch_array($result)){
array_push($response,array("zonenumber"=>$roww[0]));
}
echo json_encode(array("server_response2"=>$response));
mysqli_close($connectionn);
?>
android retrofit android-arrayadapter android-spinner onitemselectedlistener
android retrofit android-arrayadapter android-spinner onitemselectedlistener
edited Nov 7 at 9:17
asked Nov 7 at 2:40
Arvind
66
66
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26
add a comment |
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can use
int position
that is method parameter instead of
int select = list_item.getSelectedItemPosition();
Edit 1:
Your code is not clear yet,but I think you did not use first spinner selected value. You load second spinner like first spinner. And you load third spinner like that also.
These are your initialize list codes
for (int i = 0; i < listViewReqDao.getServerResponse().size(); i++) {
items[i] = listViewReqDao.getServerResponse().get(i).getConverternumber();
}
final String items1 = new String[listViewReqDao.getServerResponse().size()];
for (int j = 0; j < listViewReqDao.getServerResponse().size(); j++) {
items1[j] = listViewReqDao.getServerResponse().get(j).getZonenumber();
}
String items2 = new String[listViewReqDao.getServerResponse().size()];
for (int k = 0; k < listViewReqDao.getServerResponse().size(); k++) {
items2[k] = listViewReqDao.getServerResponse().get(k).getCodeid();
}
There is not any filtered. the three array is loaded same.
Your code must be like that for second spinner.
List<YourModel> yourModelZoneList = listViewReqDao.getZonesByConverterNumber(firstSpinner.getSelectedItemPosition());
final String items1 = new String[yourModelZoneList.size()];
for (int j = 0; j < yourModelZoneList.size(); j++) {
items1[j] = yourModelZoneList.getZonenumber();
}
Also you must load third spinner list like second spinner.
List<YourModel> yourModelCodeIdList = listViewReqDao.getCodeIdsByZoneNummer(secondSpinner.getSelectedItemPosition());
final String items2 = new String[yourModelCodeIdList.size()];
for (int k = 0; k < yourModelCodeIdList.size(); k++) {
items2[k] = yourModelCodeIdList.getCodeid();
}
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use
int position
that is method parameter instead of
int select = list_item.getSelectedItemPosition();
Edit 1:
Your code is not clear yet,but I think you did not use first spinner selected value. You load second spinner like first spinner. And you load third spinner like that also.
These are your initialize list codes
for (int i = 0; i < listViewReqDao.getServerResponse().size(); i++) {
items[i] = listViewReqDao.getServerResponse().get(i).getConverternumber();
}
final String items1 = new String[listViewReqDao.getServerResponse().size()];
for (int j = 0; j < listViewReqDao.getServerResponse().size(); j++) {
items1[j] = listViewReqDao.getServerResponse().get(j).getZonenumber();
}
String items2 = new String[listViewReqDao.getServerResponse().size()];
for (int k = 0; k < listViewReqDao.getServerResponse().size(); k++) {
items2[k] = listViewReqDao.getServerResponse().get(k).getCodeid();
}
There is not any filtered. the three array is loaded same.
Your code must be like that for second spinner.
List<YourModel> yourModelZoneList = listViewReqDao.getZonesByConverterNumber(firstSpinner.getSelectedItemPosition());
final String items1 = new String[yourModelZoneList.size()];
for (int j = 0; j < yourModelZoneList.size(); j++) {
items1[j] = yourModelZoneList.getZonenumber();
}
Also you must load third spinner list like second spinner.
List<YourModel> yourModelCodeIdList = listViewReqDao.getCodeIdsByZoneNummer(secondSpinner.getSelectedItemPosition());
final String items2 = new String[yourModelCodeIdList.size()];
for (int k = 0; k < yourModelCodeIdList.size(); k++) {
items2[k] = yourModelCodeIdList.getCodeid();
}
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
add a comment |
up vote
0
down vote
You can use
int position
that is method parameter instead of
int select = list_item.getSelectedItemPosition();
Edit 1:
Your code is not clear yet,but I think you did not use first spinner selected value. You load second spinner like first spinner. And you load third spinner like that also.
These are your initialize list codes
for (int i = 0; i < listViewReqDao.getServerResponse().size(); i++) {
items[i] = listViewReqDao.getServerResponse().get(i).getConverternumber();
}
final String items1 = new String[listViewReqDao.getServerResponse().size()];
for (int j = 0; j < listViewReqDao.getServerResponse().size(); j++) {
items1[j] = listViewReqDao.getServerResponse().get(j).getZonenumber();
}
String items2 = new String[listViewReqDao.getServerResponse().size()];
for (int k = 0; k < listViewReqDao.getServerResponse().size(); k++) {
items2[k] = listViewReqDao.getServerResponse().get(k).getCodeid();
}
There is not any filtered. the three array is loaded same.
Your code must be like that for second spinner.
List<YourModel> yourModelZoneList = listViewReqDao.getZonesByConverterNumber(firstSpinner.getSelectedItemPosition());
final String items1 = new String[yourModelZoneList.size()];
for (int j = 0; j < yourModelZoneList.size(); j++) {
items1[j] = yourModelZoneList.getZonenumber();
}
Also you must load third spinner list like second spinner.
List<YourModel> yourModelCodeIdList = listViewReqDao.getCodeIdsByZoneNummer(secondSpinner.getSelectedItemPosition());
final String items2 = new String[yourModelCodeIdList.size()];
for (int k = 0; k < yourModelCodeIdList.size(); k++) {
items2[k] = yourModelCodeIdList.getCodeid();
}
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use
int position
that is method parameter instead of
int select = list_item.getSelectedItemPosition();
Edit 1:
Your code is not clear yet,but I think you did not use first spinner selected value. You load second spinner like first spinner. And you load third spinner like that also.
These are your initialize list codes
for (int i = 0; i < listViewReqDao.getServerResponse().size(); i++) {
items[i] = listViewReqDao.getServerResponse().get(i).getConverternumber();
}
final String items1 = new String[listViewReqDao.getServerResponse().size()];
for (int j = 0; j < listViewReqDao.getServerResponse().size(); j++) {
items1[j] = listViewReqDao.getServerResponse().get(j).getZonenumber();
}
String items2 = new String[listViewReqDao.getServerResponse().size()];
for (int k = 0; k < listViewReqDao.getServerResponse().size(); k++) {
items2[k] = listViewReqDao.getServerResponse().get(k).getCodeid();
}
There is not any filtered. the three array is loaded same.
Your code must be like that for second spinner.
List<YourModel> yourModelZoneList = listViewReqDao.getZonesByConverterNumber(firstSpinner.getSelectedItemPosition());
final String items1 = new String[yourModelZoneList.size()];
for (int j = 0; j < yourModelZoneList.size(); j++) {
items1[j] = yourModelZoneList.getZonenumber();
}
Also you must load third spinner list like second spinner.
List<YourModel> yourModelCodeIdList = listViewReqDao.getCodeIdsByZoneNummer(secondSpinner.getSelectedItemPosition());
final String items2 = new String[yourModelCodeIdList.size()];
for (int k = 0; k < yourModelCodeIdList.size(); k++) {
items2[k] = yourModelCodeIdList.getCodeid();
}
You can use
int position
that is method parameter instead of
int select = list_item.getSelectedItemPosition();
Edit 1:
Your code is not clear yet,but I think you did not use first spinner selected value. You load second spinner like first spinner. And you load third spinner like that also.
These are your initialize list codes
for (int i = 0; i < listViewReqDao.getServerResponse().size(); i++) {
items[i] = listViewReqDao.getServerResponse().get(i).getConverternumber();
}
final String items1 = new String[listViewReqDao.getServerResponse().size()];
for (int j = 0; j < listViewReqDao.getServerResponse().size(); j++) {
items1[j] = listViewReqDao.getServerResponse().get(j).getZonenumber();
}
String items2 = new String[listViewReqDao.getServerResponse().size()];
for (int k = 0; k < listViewReqDao.getServerResponse().size(); k++) {
items2[k] = listViewReqDao.getServerResponse().get(k).getCodeid();
}
There is not any filtered. the three array is loaded same.
Your code must be like that for second spinner.
List<YourModel> yourModelZoneList = listViewReqDao.getZonesByConverterNumber(firstSpinner.getSelectedItemPosition());
final String items1 = new String[yourModelZoneList.size()];
for (int j = 0; j < yourModelZoneList.size(); j++) {
items1[j] = yourModelZoneList.getZonenumber();
}
Also you must load third spinner list like second spinner.
List<YourModel> yourModelCodeIdList = listViewReqDao.getCodeIdsByZoneNummer(secondSpinner.getSelectedItemPosition());
final String items2 = new String[yourModelCodeIdList.size()];
for (int k = 0; k < yourModelCodeIdList.size(); k++) {
items2[k] = yourModelCodeIdList.getCodeid();
}
edited Nov 7 at 7:17
answered Nov 7 at 5:56
enesgonez
514
514
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
add a comment |
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 8 at 11:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182903%2fonitemselectedlistener-not-responding-for-spinner-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is the need of list1 array in selectcode.java class when you're not using it anywhere in your code so comment that line on which you're getting error
– Shehroz
Nov 7 at 9:23
I commented it in the actual code. I can populate just spinner1, but spinner 2 and 3 are not. Can you suggest an edit @Shehroz
– Arvind
Nov 7 at 9:26