我有json
响应,如:
{
"Users":[
{
"username":"Varun",
"userid":"626"
}
]
}
我想在spinner
中添加一个用户,并让用户ID在这里传递给服务器。
spinnerUser=new ArrayList<>();
//spinnerUser=new ArrayList<>();
//display first question to the user
try {
JSONArray jsonArray = (JSONArray) response.get("Users");
for (int i=0;i<jsonArray.length();i++)
{
JSONObject json=jsonArray.getJSONObject(i);
{
/*User userData=new User();
userData.setUsername(json.optString("username"));
userData.setUserid(json.optString("userid"));
jsonSpinner.add(userData);
spinnerUser.add(json.optString("username"));*/
String name = json.optString("username").toString();
userid=json.optString("userId").toString();
spinnerUser.add(name);
}
}
}catch (Exception e)
{
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
R.layout.simple_spinner_item, spinnerUser);
adapter.setDropDownViewResource(R.layout.simple_spinner_item);
user.setAdapter(adapter);
我怎样才能实现这一目标。有关此方面的任何帮助请建议我。
答案 0 :(得分:3)
首先创建一个 POJO类用户
public class User {
private String name;
private String userId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return name;
}
}
然后更改下面的代码
List<User> userList = new ArrayList<>();
spinnerUser = new ArrayList<>();
//display first question to the user
try {
JSONArray jsonArray = (JSONArray) response.get("Users");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
String name = json.optString("username").toString();
String userid = json.optString("userid").toString();
User user = new User();
user.setName(name);
user.setUserId(userid);
userList.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
ArrayAdapter<User> adapter = new ArrayAdapter<User>(getContext(), R.layout.simple_spinner_item, userList);
adapter.setDropDownViewResource(R.layout.simple_spinner_item);
user.setAdapter(adapter);
用于访问所选的spinner
项目。
尝试以下代码
User selectedUser=(User) user.getSelectedItem();
Log.e("user Name", selectedUser.getName());
Log.e("user id", selectedUser.getuserId());
答案 1 :(得分:1)
试试这个:
将值和密钥存储在HashMap
private HashMap<String, String> hmapcat = new HashMap<>();
String name = json.optString("username").toString();
String userid=json.optString("userId").toString();
hmapcat.put(name,userid);
然后Onclick
Spinner
您将获得ID,请尝试以下逻辑。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
Log.i("Selected item : ", items);
String id1=hmapcat.get(items ); // here you will get ids
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
我希望这会对你有所帮助
答案 2 :(得分:0)
用户ID中的“I”在json对象中很小。
userid=json.optString("userid").toString();
public class User()
{
@SerializedName("userid")
private String userid;
@SerializedName("userName")
private String userName;
//define getter setter
}
用户Gson获取List。 How to parse json parsing Using GSON in android