2011-12-07 6 views
4

私はのOnItemClickListenerを私のGridviewの項目に設定しようとしています。何らかの理由で、リスナー内のonItemCLickメソッドが呼び出されることはありません。リスナーおよびアダプタの設定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; 
    } 
} 

。このコードは私のために他の多くの場所で動作します。

+1

あなたのgridViewのonClickListenerのための1つの閉じ括弧が欠けていたと思います。 – user370305

+0

ur importsが適切かどうかチェックされていますか? –

+0

かっこやかっこが間違っていれば、正しくコンパイルされませんでしたか?ちょうどインポートをチェックし、すべてが正しいように見えます。 – dbaugh

答えて

6

同じ問題がありました。私の状況で

、 "アンドロイド:フォーカス可能= true" を私のGridList項目レイアウトのブロック項目をクリックしてイベント

<TextView android:id="@+id/auction_item_title" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:singleLine="true" 
    ***android:focusable="true"*** 
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever" 
/> 

私は[アンドロイド:フォーカス可能= "true" を]削除し、問題が解決され、

+0

問題を保存するには、 'android:focusable =" false "'と 'android:focusableInTouchMode =" false "'を追加する必要があります – Houcine

3

実際には、グリッド内の項目がクリック可能でないことを確認するだけです。 GridViewはクリック可能なアイテムを処理できません。例えば、ボタンは機能しません。また、LinearLayoutやその他のクリック可能でないアイテムからアイテムを作成した場合は、clickable = "true"というプロパティを設定していないことを確認する必要があります。