已搜索listview OnClick不工作页面,不解决。
my Activity通过调用自定义适配器来填充listview MyArrayAddapter 它扩展了ArrayAdapter类。
问题:onItemClick()或setOnItemClick()没有响应
当我单击任何项时,没有任何动作发生,甚至不执行日志语句。
& lt; & lt;>>
代码片段:
在我的类中扩展Activity实现OnItemClickListener, 我初始化并调用OnCreate()方法中的以下函数:
private void listViewSet() {
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( this, "Component "+ rootListWrapper.get( position ).toString() + " clicked.", Toast.LENGTH_LONG ).show();
root = rootListWrapper.get( position );
rootListWrapper = root.children;
this.listViewSet();
}
注意:当我点击列表视图的一个项目,我得到完全相同的消息,只是在时间戳不同。我收到7-8条这样的新信息。LogCat之前点击:
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
点击后:
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
活动的布局:
<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>
行项布局:
<?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>
它第一次完美地填充,但我希望当我单击时,我应该能够通过改变参数和再次调用setAdapter()方法来重新填充它。
解决这个问题肯定会帮助很多人,因为我看到很多人都有未解决的疑问。
java文件
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更新:在3个按钮上使用focusable:false属性后。
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)
编辑:
将您的行xml文件替换为
<?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>
您的自定义适配器可能有一个可聚焦的项目,如(EditText, Button)等,尝试将行项目的焦点设置为false
listView.performClick();
将触发listview上的onClick(),而不是列表中的单个项目。
所以,你还应该实现onClick()方法拦截点击在你的listview