The problem is when I select any university I got the name set on the autocomplete textview. But I am also looking for their ids which are fetched from server in key value pair:
"u_id": "1","university_name": "RGPV"`
I want this u_id
as well with name
. But I am only getting this name. I need the u-id
for further purpose.
How do I get this id as well?
Here is the code for fetching json
class FetchUniversitytask extends AsyncTask<Void,Void ,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
/* progressDialog = new ProgressDialog(ActivitySignUp.this);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Please Wait...");
progressDialog.show();*/
linearLayout.setVisibility(View.VISIBLE);
ObjectAnimator flip = ObjectAnimator.ofFloat(imgSpoon, "rotationY", 0f, 180f);
flip.setDuration(800);
flip.setRepeatCount(Animation.INFINITE);
flip.start();
}
@Override
protected String doInBackground(Void... params) {
String result = WebServiceUtil.getInstance(ActivitySignUp.this).getJsonFromGetMethod(URLconstant.UNIVERSITY);
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//progressDialog.dismiss();
linearLayout.setVisibility(View.INVISIBLE);
if (null != result) {
try {
final Universitydata univHolder = new Gson().fromJson(result, Universitydata.class);
if ( null != univHolder) {
// Log.d("uninholder", "" + univHolder.toString());
// Toast.makeText(ActivitySignUp.this, univHolder.toString(), Toast.LENGTH_SHORT).show();
Universitylist = new ArrayList<>();
for (University university : univHolder.getData()) {
if (!university.getUniversityName().isEmpty()) {
Universitylist.add(university.getUniversityName());
}
}
if (Universitylist.size() > 0) {
/* universityAdapter = new ArrayAdapter<String>(ActivitySignUp.this, R.layout.spinner_item, Universitylist);
universityAdapter.setDropDownViewResource(R.layout.spinner_itemforchild);*/
sadapter = new ArrayAdapter<String>(ActivitySignUp.this, android.R.layout.simple_dropdown_item_1line, Universitylist);
suuniversity.setThreshold(1);//will start working from first character
suuniversity.setAdapter(sadapter);//setting the adapter data into the AutoCompleteTextView
suuniversity.setTextColor(getColor(R.color.colorPrimary));
suuniversity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("univ", String.valueOf(adapterView.getItemAtPosition(i)));
Log.d("univ", String.valueOf(adapterView));
String data = (String)adapterView.getItemAtPosition(i);
int realPosition = Universitylist.indexOf(data);
University un = new University();
un.getUId();
u_id = String.valueOf(realPosition+1);
Log.d("univ_id",u_id);
fetchCollegeFromServerByUniversityName(u_id);
}
});
}
} else {
Toast.makeText(ActivitySignUp.this, "Something not right 1", Toast.LENGTH_SHORT).show();
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
Toast.makeText(ActivitySignUp.this, "Something not right 2", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(ActivitySignUp.this, "Something not right 3", Toast.LENGTH_SHORT).show();
}
}
}
here is my json code
{
"data": [
{
"u_id": "1",
"university_name": "RGPV"
},
{
"u_id": "2",
"university_name": "Samrat Ashok Technological Institute, Vidisha"
},
{
"u_id": "3",
"university_name": "Jabalpur Engineering College"
},
{
"u_id": "4",
"university_name": "Rewa Engineering College"
},
{
"u_id": "5",
"university_name": "University Institute of Technology, Shahdol\t"
},
{
"u_id": "6",
"university_name": "Indira Gandhi Engineering College, Sagar"
},
{
"u_id": "7",
"university_name": "Ujjain Engineering College"
},
{
"u_id": "8",
"university_name": "Shri Govindram Seksaria Institute of Technology and Science (SGSITS), Indore"
},
{
"u_id": "9",
"university_name": "University Institute of Technology, Jhabua"
},
{
"u_id": "10",
"university_name": "Madhav Institute of Technology & Science, Gwalior"
},
{
"u_id": "11",
"university_name": "University Institute of Technology, Bhopal"
}
]
}
The fact that you only have university.getUniversityName()
added to the adapter means that you won't be able to extract the ID.
You should make an ArrayAdapter<University>
and implement a toString method in the University class which returns the name (and optionally the ID, if you want to see it)
List<University> universities = new ArrayList<>();
ArrayAdapter<University> adapter = new ArrayAdapter<>(ActivitySignUp.this, R.layout.universiry_dropdown_layout, universities);
suuniversity.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_itemforchild);
for (University university : univHolder.getData()) {
if (!university.getUniversityName().isEmpty()) {
adapter.add(university);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.