-2
我從MongoDB發出http請求,併成功顯示我從我的請求中獲得的列表。現在,當我點擊ListView項時,我得到用戶全名,但我無法獲得用戶ID(Mongo ID)。我怎樣才能做到這一點?無法獲取ListView所選項目屬性
用戶等級:
public class User implements Serializable {
private String user_id;
private String name;
private String username;
private String password;
public User(String user_id, String name, String username, String password) {
this.nom_prenom = nom_prenom;
this.username = username;
this.password = password;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在我的活動,我把:
for (int i = 0; i < data.length(); i++) {
try {
JSONObject userJson = data.getJSONObject(i);
if (userJson.has("_id") && userJson.has("name") && userJson.has("password")
&& userJson.has("username")) {
JSONObject idJson = userJson.getJSONObject("_id");
String user_id = idJson.getString("$oid");
String name = userJson.getString("name");
String username = userJson.getString("username");
String password = userJson.getString("password");
rentalProperties.add(new user(user_id, name, username, password));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
//...
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();
UsersFragment usersFrag = UsersFragment.newInstance(rentalProperties);
fragTrans.replace(R.id.fragment_view, usersFrag, "MY_FRAGMENT");
fragTrans.commit();
mfragment_view.setVisibility(View.VISIBLE);
我還適配器基於 「rentalProperties」。我得到的用戶名是這樣的:
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
User obj = adapter.getItem(position);
String user_id = (String) obj.getUser_id();
String name = (String) obj.getName();
Log.e("User", user_id);
Intent intent = new Intent(getContext(), MainDisponibilitesActivity.class);
intent.putExtra(UserChoosedId, user_id);
intent.putExtra(UserChoosedName, name);
startActivity(intent);
}
我希望這能說明我的情況。我有太多的文件。 謝謝
發佈您的代碼.... –
我解決了它,我只是忘記在User類的構造函數中添加user_id。抱歉。謝謝你們 – Cutis