我正在尝试为我的Gridview中的项目设置一个OnItemClickListener。但是由于某些原因,监听器内的onItemClick方法从未被调用。
设置监听器和适配器:
UsersAdapter usersAdapter = new UsersAdapter(venueUsers);
gridView.setAdapter(usersAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(Users.this, com.roqbot.client.login.Profile.class);
intent.putExtra("idUser", id);
startActivity(intent);
}
});
我的适配器:
private class UsersAdapter extends BaseAdapter implements ListAdapter {
private JSONArray users;
private UsersAdapter(JSONArray users) {
this.users = users;
}
public int getCount() {
return users.length();
}
public JSONObject getItem(int position) {
return users.optJSONObject(position);
}
public long getItemId(int position) {
return users.optJSONObject(position).optInt("idUser");
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = Users.this.getLayoutInflater().inflate(R.layout.user_icon, null);
JSONObject user = getItem(position);
TextView username = (TextView) convertView.findViewById(R.id.username);
username.setText(user.optString("sName"));
TextView userScore = (TextView) convertView.findViewById(R.id.userScore);
int iDJScore = user.optInt("iDJScore");
if (iDJScore > 0) {
userScore.setText(Integer.toString(iDJScore));
}
else {
userScore.setVisibility(TextView.INVISIBLE);
((ImageView) convertView.findViewById(R.id.userScoreBg)).setVisibility(View.INVISIBLE);
}
TextView userLevel = (TextView) convertView.findViewById(R.id.userLevel);
userLevel.setText(user.optString("iDJLevel"));
TextView userMatch = (TextView) convertView.findViewById(R.id.userMatch);
ImageView matchIcon = (ImageView) convertView.findViewById(R.id.matchIcon);
int iCompatibility = user.optInt("iCompatibility");
if (iCompatibility != 0) {
userMatch.setText( iCompatibility + "%");
}
else {
userMatch.setVisibility(TextView.INVISIBLE);
matchIcon.setVisibility(ImageView.INVISIBLE);
}
ImageView userIcon = (ImageView) convertView.findViewById(R.id.userIcon);
String sUserIcon = user.optString("sImageUrl-thumb");
imageLoader.DisplayImage(sUserIcon, Users.this, userIcon);
return convertView;
}
}
我对为什么点击监听器不起作用感到困惑。这段代码在其他很多地方都能正常工作。