2015-04-01 79 views
-3

我的问题是当我点击主要布局中的列表项时,有时会触发但不是全部时间。 我不知道如何解决这个问题? 请帮助我..Android Listview OnClickListener无法正常工作

我的代码是:

View v = convertView.findViewById(R.id.chat_window_single); 
v.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.i("position",v.getTag().toString()); 
     SharedPreferences.Editor editor = sharedpreferences.edit(); 
     editor.putLong("eventId", groupEventMoList.get(Integer.parseInt(v.getTag().toString())).getEventId()); 
     editor.putString("eventTitle",groupEventMoList.get(Integer.parseInt(v.getTag().toString())).getText()); 
     editor.commit(); 
     Intent groupAct = new Intent(context, GroupChatActivity.class); 
     startActivity(groupAct); 
    } 

}); 

我的布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/chat_window_single" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:clickable="false" 
android:focusable="false" 
android:focusableInTouchMode="false" 
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants"> 

<TextView 
    android:id="@+id/chat_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5sp" 
    android:shadowDx="1" 
    android:shadowDy="1" 
    android:textColor="@android:color/primary_text_light" 
    android:textSize="20sp" > 
</TextView> 

<TextView 
    android:id="@+id/event_place" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5sp" 
    android:shadowDx="1" 
    android:shadowDy="1" 
    android:textColor="@android:color/primary_text_light" 
    android:textSize="20sp" > 
</TextView> 

<TextView 
    android:id="@+id/event_date" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5sp" 
    android:shadowDx="1" 
    android:shadowDy="1" 
    android:textColor="@android:color/primary_text_light" 
    android:textSize="20sp" > 
</TextView> 

</LinearLayout> 

我主要布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<ListView 
    android:id="@+id/chat_list_view" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="80dp" > 

</ListView> 
</LinearLayout> 

任何帮助,将不胜感激。

+0

你必须使用的,而不是Onclicklistener – Shriram 2015-04-01 06:12:38

+0

onItemClickListener发表您的列表视图中点击lisener代码。并使用linerlayout而不是视图。在getview方法中。 – Shvet 2015-04-01 06:14:29

回答

2

你textviews可能会抢走父列表视图的焦点,使他们clickablefocusable = false也upermost线性布局需要android:descendantFocusability="blocksDescendants"

+0

是的,我已经添加在布局文件 – nmkkannan 2015-04-01 06:16:41

+0

您的文本视图仍然'可点击=真' – Milen 2015-04-01 06:17:57

+0

为什么不使用'onItemClickListener'为ListView? – 2015-04-01 06:18:47

相关问题