问题是当我选择任何大学时,我在自动完成文本视图中设置了名称。但我也在寻找从密钥值对中的服务器获取的ID:
"u_id": "1","university_name": "RGPV"`
我想要u_id
以及name
。但我只是得到这个名字。我需要u-id
用于进一步的目的。
我如何获得此ID?
以下是获取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();
}
}
}
这是我的json代码
{
"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"
}
]
}
答案 0 :(得分:0)
您只有university.getUniversityName()
添加到适配器的事实意味着您无法提取ID。
您应该创建一个ArrayAdapter<University>
并在University类中实现一个toString方法,该方法返回名称(如果您想查看它,还可以选择ID)
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);
}
}