OnItemClick不响应点击 [英] OnItemClick not responding to clicks
问题描述
已经搜索<一个href=\"http://stackoverflow.com/questions/15334893/android-horizontallistview-onitemclick-not-working\">listview不是的OnClick <一个href=\"http://stackoverflow.com/questions/14308192/android-programming-onitemclicklistener-for-multiple-listviews-doesnt-work\">working网页的,并没有解决。
我的活动通过调用自定义适配器的ListView填充
的 MyArrayAddapter 的
延伸的 ArrayAdapter 的类。
问题:onItemClick()或setOnItemClick()未响应
。当我在没有任何动作发生的项目点击,甚至没有日志语句得到执行。
&LT;&LT; >>
code片断:
在我的课堂延伸活动实现OnItemClickListener,我初始化和呼叫跟随的OnCreate函数()方法:
私人无效listViewSet(){ MyArrayAdapter适配器=新MyArrayAdapter(这一点,R.id.tvrow,rootListWrapper);
//指定适配器的ListView
listView.setAdapter(适配器);
listView.setOnItemClickListener(本); // ******
listView.performClick();
}
即使敬酒犯规显示:(
@覆盖
公共无效onItemClick(适配器视图&LT;&GT;母公司,视图V,INT位置,长的id){
Log.v(的onclick,asdfhjdf);
Toast.makeText(这一点,分量+ rootListWrapper.get(位置)的ToString()+点击,Toast.LENGTH_LONG).show(); 根= rootListWrapper.get(位置);
rootListWrapper = root.children;
this.listViewSet();
}
请注意:当我在列表视图的项目点击,我只得到在不同的时间戳完全相同的信息。我得到这样的7-8新消息。 LogCat中前点击:
九月3日至15日:54:06.507:W /微量(1825):从nativeGetEnabledTags意外的值:0
9月3日至一十五日:54:06.877:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:06.877:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:06.947:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:06.947:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:07.117:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:07.117:W /微量(1825):意外从nativeGetEnabledTags值:0
9月3日至一十五日:54:07.297:W /微量(1825):意外从nativeGetEnabledTags值:0
点击后:
10月3日至15日:05:36.458:W /微量(1825):从nativeGetEnabledTags意外的值:0
10月3日至15日:05:36.458:W /微量(1825):意外从nativeGetEnabledTags值:0
10月3日至15日:05:36.480:W /微量(1825):意外从nativeGetEnabledTags值:0
10月3日至15日:05:40.817:W /微量(1825):意外从nativeGetEnabledTags值:0
10月3日至15日:05:40.817:W /微量(1825):意外从nativeGetEnabledTags值:0
10月3日至15日:05:40.917:W /微量(1825):意外从nativeGetEnabledTags值:0
10月3日至15日:05:40.960:W /微量(1825):意外从nativeGetEnabledTags值:0
该活动的布局:
&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=大类。&GT; &LT; ListView控件
机器人:ID =@ + ID / MYLIST
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT&GT;
&LT; /&的ListView GT;&LT; / RelativeLayout的&GT;
布局行项目:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent 机器人:填充=20dp
&GT; &LT; TextView的机器人:ID =@ + ID / tvrow
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT机器人:文本=组件/&GT; &LT;的LinearLayout机器人:weightSum =9的android:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID / tvrow&GT; &LT;按钮机器人:ID =@ + ID / BL
机器人:layout_weight =4
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ blue_right
/&GT;
&LT;按钮
机器人:ID =@ + ID / BC
机器人:layout_weight =1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ COMPONENT2
/&GT;
&LT;按钮
机器人:layout_weight =4
机器人:ID =@ + ID / BR
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ blue_right
/&GT;
&LT; / LinearLayout中&GT;
&LT; / RelativeLayout的&GT;
它完美地填充了第一次,但我想,当我点击我应该能够通过改变参数,并再次调用setAdapter()方法来重新填充它。
解决这必将助阵许多人,因为我看到许多未解有疑惑。
有关问题的Java类文件:
公共类大类扩展活动实现OnItemClickListener {
清单&LT;节点&GT; rootListWrapper;
节点根;
ListView控件的ListView;
GetMethodEx getJSONString;
的JSONObject jObject;
大类HealthNext;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_health_display); ListView控件=(ListView控件)findViewById(R.id.mylist);
rootListWrapper =新的ArrayList&LT;节点&GT;();
根=新节点();
getJSONString =新GetMethodEx();
jObject =新的JSONObject(); //值=新的String [] {机器人,iPhone,的WindowsMobile
//黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X
//的Linux,OS / 2}; this.jsonDataFetch();
this.listViewSet(); }
私人无效jsonDataFetch(){
// TODO自动生成方法存根
尝试{
jObject =新的JSONObject(getJSONString.getInternetData());
根= root.createNode(jObject);
Log.v(部件,jObject.getString(身份));
rootListWrapper.add(根);
}赶上(例外五){
e.printStackTrace();
}
Log.v(部件,rootListWrapper.toString());
}
私人无效listViewSet(){
//定义一个新的适配器
//第一个参数 - 上下文
//第二个参数 - 布局行
//第三参数 - TextView的编号到的数据被写入
//四 - 数据数组 MyArrayAdapter适配器=新MyArrayAdapter(这一点,R.id.tvrow,rootListWrapper);
//指定适配器的ListView
listView.setAdapter(适配器);
listView.setOnItemClickListener(本);
listView.performClick();
} @覆盖
公共无效onItemClick(适配器视图&LT;&GT;母公司,视图V,INT位置,长的id){
Log.v(的onclick,asdfhjdf);
Toast.makeText(BigClass.this,分量+ rootListWrapper.get(位置)的ToString()+点击,Toast.LENGTH_LONG).show(); 根= rootListWrapper.get(位置);
rootListWrapper = root.children;
HealthNext.jsonDataFetch();
HealthNext.listViewSet();
}}
logcat的更新:使用后可获得焦点:false属性的3个按键
10月3日至15日:57:44.798:V / ONCLICK(958):asdfhjdf
10月3日至15日:57:44.952:D / AndroidRuntime(958):关闭VM
10月3日至15日:57:44.952:W / dalvikvm(958):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a70930)
10月3日至15日:57:44.992:E / AndroidRuntime(958):致命异常:主要
10月3日至15日:57:44.992:E / AndroidRuntime(958):显示java.lang.NullPointerException
10月3日至15日:57:44.992:E / AndroidRuntime(958):在com。示例@@@@@@ BigClass.onItemClick(BigClass.java:86)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.widget.AdapterView.performItemClick(AdapterView.java:298)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2749)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.widget.AbsListView $ 1.run(AbsListView.java:3423)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.os.Handler.handleCallback(Handler.java:725)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.os.Handler.dispatchMessage(Handler.java:92)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.os.Looper.loop(Looper.java:137)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在android.app.ActivityThread.main(ActivityThread.java:5039)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在java.lang.reflect.Method.invokeNative(本机方法)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在java.lang.reflect.Method.invoke(Method.java:511)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10月3日至15日:57:44.992:E / AndroidRuntime(958):在dalvik.system.NativeStart.main(本机方法)
编辑:
这个替换您的行xml文件
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:descendantFocusability =真
机器人:填充=20dp
&GT; &LT; TextView的机器人:ID =@ + ID / tvrow
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT机器人:文本=组件/&GT; &LT;的LinearLayout机器人:weightSum =9的android:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID / tvrow&GT; &LT;按钮机器人:ID =@ + ID / BL
机器人:layout_weight =4
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:可聚焦=假
机器人:背景=@绘制/ blue_right
/&GT;
&LT;按钮
机器人:ID =@ + ID / BC
机器人:layout_weight =1
机器人:可聚焦=假
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ COMPONENT2
/&GT;
&LT;按钮
机器人:layout_weight =4
机器人:ID =@ + ID / BR
机器人:可聚焦=假
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ blue_right
/&GT;
&LT; / LinearLayout中&GT;
&LT; / RelativeLayout的&GT;
您自定义适配器可能有一个聚焦的项目,如(EditText上,巴顿)等,尝试设置的排项目重点为false
have searched listview OnClick not working pages, doesn't solve.
my Activity populates listview by calling a custom adapter MyArrayAddapter which extends ArrayAdapter class.
The problem: onItemClick() or setOnItemClick() ain't responding.
when i click on any of the items no action takes place, not even log statements get executed.
<<>>
code snippet:
Under my class which extends Activity implements OnItemClickListener, I initialize and call following functions in OnCreate() method:
private void listViewSet() {
MyArrayAdapter adapter = new MyArrayAdapter( this, R.id.tvrow, rootListWrapper );
// Assign adapter to ListView
listView.setAdapter( adapter );
listView.setOnItemClickListener( this ); //******
listView.performClick();
}
Even toast doesnt show up :(
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v( "OnCLick", "asdfhjdf" );
Toast.makeText( this, "Component "+ rootListWrapper.get( position ).toString() + " clicked.", Toast.LENGTH_LONG ).show();
root = rootListWrapper.get( position );
rootListWrapper = root.children;
this.listViewSet();
}
Note: when i click on an item of the listview, i get exactly same messages only differing in timestamp. I get 7-8 such new messages. LogCat before click:
03-15 09:54:06.507: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.877: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.877: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.947: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.947: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.117: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.117: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.297: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
after click:
03-15 10:05:36.458: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:36.458: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:36.480: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.817: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.817: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.917: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.960: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
The layout for the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BigClass" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
layout for row items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<TextView android:id="@+id/tvrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Component" />
<LinearLayout android:weightSum="9" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvrow" >
<Button android:id="@+id/bL"
android:layout_weight="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/blue_right"
/>
<Button
android:id="@+id/bC
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/component2"
/>
<Button
android:layout_weight="4"
android:id="@+id/bR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/blue_right"
/>
</LinearLayout>
</RelativeLayout>
It populates perfectly for first time, but I want when I click i should be able to repopulate it by changing the parameters and calling the setAdapter() method again.
Solving this will surely help out many others, as i see many have unsolved doubts.
java file for class in question:
public class BigClass extends Activity implements OnItemClickListener{
List<Node> rootListWrapper;
Node root;
ListView listView;
GetMethodEx getJSONString;
JSONObject jObject;
BigClass HealthNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health_display);
listView = (ListView) findViewById(R.id.mylist);
rootListWrapper = new ArrayList<Node>();
root = new Node();
getJSONString = new GetMethodEx();
jObject = new JSONObject();
//values = new String[] { "Android", "iPhone", "WindowsMobile",
// "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
// "Linux", "OS/2" };
this.jsonDataFetch();
this.listViewSet();
}
private void jsonDataFetch() {
// TODO Auto-generated method stub
try{
jObject=new JSONObject( getJSONString.getInternetData() );
root = root.createNode( jObject );
Log.v( "component", jObject.getString("status") );
rootListWrapper.add( root );
}catch(Exception e){
e.printStackTrace();
}
Log.v("component", rootListWrapper.toString());
}
private void listViewSet() {
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Fourth - the Array of data
MyArrayAdapter adapter = new MyArrayAdapter( this, R.id.tvrow, rootListWrapper );
// Assign adapter to ListView
listView.setAdapter( adapter );
listView.setOnItemClickListener( this );
listView.performClick();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v( "OnCLick", "asdfhjdf" );
Toast.makeText( BigClass.this, "Component "+ rootListWrapper.get( position ).toString() + " clicked.", Toast.LENGTH_LONG ).show();
root = rootListWrapper.get( position );
rootListWrapper = root.children;
HealthNext.jsonDataFetch();
HealthNext.listViewSet();
}
}
LOGCAT updated: after using focusable:false attribute on 3 buttons.
03-15 10:57:44.798: V/OnCLick(958): asdfhjdf
03-15 10:57:44.952: D/AndroidRuntime(958): Shutting down VM
03-15 10:57:44.952: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-15 10:57:44.992: E/AndroidRuntime(958): FATAL EXCEPTION: main
03-15 10:57:44.992: E/AndroidRuntime(958): java.lang.NullPointerException
03-15 10:57:44.992: E/AndroidRuntime(958): at com.example.@@@@@@.BigClass.onItemClick(BigClass.java:86)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.os.Handler.handleCallback(Handler.java:725)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.os.Looper.loop(Looper.java:137)
03-15 10:57:44.992: E/AndroidRuntime(958): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-15 10:57:44.992: E/AndroidRuntime(958): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 10:57:44.992: E/AndroidRuntime(958): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 10:57:44.992: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-15 10:57:44.992: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-15 10:57:44.992: E/AndroidRuntime(958): at dalvik.system.NativeStart.main(Native Method)
Edit:
Replace your row xml file with this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="true"
android:padding="20dp"
>
<TextView android:id="@+id/tvrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Component" />
<LinearLayout android:weightSum="9" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvrow" >
<Button android:id="@+id/bL"
android:layout_weight="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:background="@drawable/blue_right"
/>
<Button
android:id="@+id/bC"
android:layout_weight="1"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/component2"
/>
<Button
android:layout_weight="4"
android:id="@+id/bR"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/blue_right"
/>
</LinearLayout>
</RelativeLayout>
Your custom adapter may have a focusable items like (EditText, Button) etc, try to set focus of row items to false
这篇关于OnItemClick不响应点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!