我想点击List Item里面的Button。我试过那些代码,它没有给我错误,但仍然没有完成这项工作。 屏幕:
我尝试了这些代码(1):
EntityListItem view2 = solo.getView(EntityListItem.class,1);
solo.clickOnView(view2.DeleteEntity);
solo.sleep(3000);
我尝试了这些代码(2):
ListView myList = (ListView)solo.getView(com.hh.android.R.id.lister);
View listElement = myList.getChildAt(0);
View alt = listElement.findViewById(com.hh.android.R.id.footer);
solo.clickOnView(alt.findViewById(com.imona.android.R.id.DeleteEntity));
我尝试了这些代码(3):
ListView myList = (ListView)solo.getView(com.hh.android.R.id.lister);
View listElement = myList.getChildAt(0);
solo.clickOnView(listElement.findViewById(com.hh.android.R.id.DeleteEntity) );
此实体列表
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lister"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp" >
</ListView>
这是列表项
<LinearLayout
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_marginRight="20dp"
android:id="@+id/DeleteEntity"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_action_delete"/>
</LinearLayout>
答案 0 :(得分:2)
使用此代码
ListView ListView=(ListView)solo.getView(R.id.listview);
View view=ListView.getChildAt(0);
Button button=(Button)view.findViewById(R.id.button);
solo.clickOnView(button);
答案 1 :(得分:1)
这解决了我的问题:
ListView myList = (ListView) solo.getView(com.hh.android.R.id.lister,1); //1 is ipmortant, dont know why
EntityListItem til = (EntityListItem) myList.getChildAt(0);
solo.clickOnView(til.DeleteEntity);
我使用列表ID获取LIstView,然后使用索引号从该ListView获取列表项对象。然后单击该项目对象视图的按钮,发送任何文本。
答案 2 :(得分:0)
我将这些方法编码为help =)
protected View getViewAtListView(int resListViewID, int position) {
return ((ListView) getSolo().getView(resListViewID)).getChildAt(position);
}
protected View getViewAtRowListView(int resListView, int position, int viewIDInRowView) {
return getViewAtListView(resListView, position).findViewById(viewIDInRowView);
}
protected void clickOnViewAtRowView(int resListView, int position, int viewIDInRowView) {
View view = getViewAtRowListView(resListView, position, viewIDInRowView);
assertNotNull(view);
getSolo().clickOnView(view);
}