我正在努力解决以下问题。我有一个列表视图,显示属于团队的用户。这些行包含用户的knickname以及自从使用Google fit SDK后每个午夜以来他走过的步数。
所以你看到Cerberus 33走了2342步和联想0。
我希望手机中登录的用户Cerberus 33只能点击一行,即第一行,这样他就可以看到一个带有离开团队按钮的提醒对话框。
这是我的代码的一部分。
我使用共享首选项从其他活动中检索登录的用户名和密码。
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(this);
username = settings.getString("username", "");
password = settings.getString("password", "");
现在我将这个登录的用户名与我通过排球库从数据库中获得的用户名进行比较。
private void getTeamPlayers(final String teamId, final String username,
final String password) {
String tag_json_obj = "json_obj_req";
final HashMap<String, String> postParams1 = new HashMap<String,
String>();
postParams1.put("team_id", teamId);
postParams1.put("username", username);
postParams1.put("password", password);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams1);
//{"team_id":"189","password":"xxx","username":"Cerberus 33"}
final JsonArrayRequest jsonObjReq1 = new
JsonArrayRequest(AppConfig.URL_GET_TEAM, jsonObject,
new com.android.volley.Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
try {
Log.d("JsonArray", response.toString());
for (int i = 0; i < response.length(); i++) {
JSONObject item =
response.getJSONObject(i);
userType = item.getString("user_type");
userName = item.getString("username");
TeamPlayers tm = new TeamPlayers();
tm.setLatestSteps(item.getString("latest_steps"));
tm.setNickname(item.getString("nickname"));
tm.setUserType(item.getString("user_type"));
String latestSteps =
item.getString("latest_steps");
String userType =
item.getString("user_type");
int totalSteps =
Integer.valueOf(latestSteps);
Log.d("total", "steps: " + totalSteps);
teamPlayersList.add(tm);
Log.d("Username",username);
Log.d("UserName",userName);
//comparing username(loggin user)
//with userName(username retrieved by database)
if(username.equals(userName)) {
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void
onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(getApplicationContext(), "It's me!!",
Toast.LENGTH_SHORT).show();
}
});
}
}
} catch (JSONException e) {
e.printStackTrace();
}
//pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
//pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq1,
tag_json_obj);
VolleySingleton.getInstance(getApplicationContext()).
addToRequestQueue(jsonObjRequest);
}
这是输出。
[{"latest_steps":"2342","user_type":"LEADER","nickname":"Cerberus
33","username":"cerberus_33@hotmail.com"},
{"latest_steps":"0","user_type":"LEADER","nickname":"Lenovo",
"username":"lenovo@gmail.com"}]
steps: 2342
D/Username﹕ cerberus_33@hotmail.com
D/UserName﹕ cerberus_33@hotmail.com
D/total﹕ steps: 0
D/Username﹕ cerberus_33@hotmail.com
D/UserName﹕ lenovo@gmail.com
D/TAG﹕ {"team_steps":"2342"}
D/team steps﹕ 2342
答案 0 :(得分:1)
我知道您希望用户只能点击自己的行。我想你有一个适配器来构建你的ListView
行。在此适配器中,覆盖方法boolean isEnabled(int position)
以仅启用所需的行。
修改强>
在适配器中添加以下内容
@Override
public boolean isEnabled(int position) {
if (position is user row) {
return true;
} else {
return false;
}
}
这将允许用户仅在您返回true的行上进行clik(位置是用户行)。然后你可以像往常一样使用listView.setOnItemClickListener()
。
修改强>
这就是我所做的,但现在都禁用了这两行。
adapter = new CustomAdapter(getApplication(), teamPlayersList){
@Override
public boolean isEnabled(int position) {
String currentName = teamPlayersList.get(position).getUserName();//i'm not sur this is the right getter but it should be
//the spirit is to compare the name of the user of this row to the name of the user you want to enabled
if (currentName.equals(userName)) {
return true;
} else {
return false;
}
}
};
}
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?>
parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"It's
me",Toast.LENGTH_SHORT).show();
}
});