无法在 android 微调器中添加提示 [英] Unable to add hint in android spinner
问题描述
我有一个 spinner
,它的行为类似于我的 android 应用程序
中的 dropdown
.我正在 android studio
中开发应用程序.微调器从 api
获取数据.在运行时,它从 API
获取 usernames
并显示在 spinner
中.当应用程序运行时,它会显示所选用户的 username
和 ID
,如下图所示
I have a spinner
that behaves like a dropdown
in my android application
. I am developing the app in android studio
. The spinner get data from the api
. At run time it gets usernames
from API
and shows in the spinner
. When app runs it shows the username
and ID
of the selected user like shown in below image
现在我想添加提示,所以我搜索了很多文章并找到了很多解决方案,我遵循了其中最简单的一个.更多理解请看下面代码
Now i want to add hint so i searched many articles and found many solutions and i follow the easiest of them. For more understanding please see the below code
try
{
JSONObject jobj = new JSONObject(jsonObject.toString());
// Locate the NodeList name
jsonArray = jobj.getJSONArray("users");
for(int i=0; i<jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);
Users user = new Users();
user.setId(jsonObject.optString("Id"));
user.setName(jsonObject.optString("Name"));
users.add(user);
userList.add("Select a username");// i add this as a hint
userList.add(jsonObject.optString("Name"));
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
现在使用 onPostExecute() 方法
@Override
protected void onPostExecute(Void args)
{
// Locate the spinner in activity_main.xml
Spinner spinner = (Spinner)findViewById(R.id.spinner);
// Spinner adapter
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, userList));
// Spinner on item click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textViewResult = (TextView)findViewById(R.id.textView);
// Set the text followed by the position
// Set the text followed by the position
if(!users.get(position).getName().equals("Select a username")) {
textViewResult.setText(" " + users.get(position - 1).getName() + " " + users.get(position - 1).getId());
}else {
textViewResult.setText("Hi " + users.get(position).getName() + " your ID is " + users.get(position).getId());
UserId = String.valueOf(users.get(position).getId());
progressDialog.dismiss();
_latitude.setText("");
_longitude.setText("");
Latitude = null;
Longitude = null;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
textViewResult.setText("");
}
});
}
当我运行应用程序时,应用程序崩溃,同时给我以下错误
When i run the application the app crashes while giving me the below error
Process: com.example.accurat.myapp, PID: 30382
java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at com.example.accurat.myapp.MainActivity$DownloadJSON$1.onItemSelected(MainActivity.java:494)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
此错误发生在 textViewResult.setText(" " + users.get(position - 1).getName() + " " + users.get(position - 1).getId());代码>
更新 1
按照Junaid Hafeez
的回答我做了以下
for(int i=0; i<jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);
Users user = new Users();
user.setId(jsonObject.optString("Id"));
user.setName(jsonObject.optString("Name"));
users.add(user);
userList.add(jsonObject.optString("Name"));
}
userList.add(0, "Select a username"); // after for loop ended i add `select a username` at `0` index
之后在 postExecute()
方法中我做了以下操作
after that in postExecute()
method i have done the following
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textViewResult = (TextView)findViewById(R.id.textView);
// Set the text followed by the position
// Set the text followed by the position
if(position>0)
{
textViewResult.setText("Hi " + users.get(position).getName() + " your ID is " + users.get(position).getId());
UserId = String.valueOf(users.get(position).getId());
_latitude.setText("");
_longitude.setText("");
Latitude = null;
Longitude = null;
}
else {
}
progressDialog.dismiss();
}
我得到的结果低于
它确实向我显示了提示,但是当我选择任何用户名时,它向我显示下一个用户名的 name
和 id
,如下图所示
It does show me the hint, but when i select any username it shows me the name
and id
of the next username as shown in below image
我坚持下去,找不到任何解决方案
I am stuck to it and couldn't find any solution
任何帮助将不胜感激.
推荐答案
可能你正在做的是,你正在点击微调器的第一项,其中 position = 0
并执行此 position - 1
在您的 setText
中,这使得实际上不存在的索引 -1
崩溃.其次你正在这样做
probably what you are doing is, you are clicking on first item of you spinner, where the position = 0
and doing this position - 1
in your setText
which makes the index -1
that is actually not exists and crashes.
Secondly you are doing this
userList.add("Select a username");// i add this as a hint
在您的循环中,在每次迭代中添加新项目.你需要做的是
in your loop, which is adding new item on each iteration. what you need to do is
一旦您拥有完整的数据列表,只需像这样在其 0 索引上添加提示
once you have complete list of your data, just add hint on its 0 index like this
userList.add(0, "Select a username");
并在您的 onClick 中,检查天气 position >0
然后做你的工作,否则当用户点击提示时忽略.
and in your onClick, check weather position > 0
then do your work otherwise ignore as user clicked on hint.
这篇关于无法在 android 微调器中添加提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!