gpt4 book ai didi

android - 使用 ArrayAdapter 和 Material v7 支持库在 Espresso 中选择对话框选项

转载 作者:行者123 更新时间:2023-11-28 20:40:41 25 4
gpt4 key购买 nike

我有一个对话框,其中包含一个包含元素的列表,其中每个元素都带有一个 ArrayAdapter:

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState){

ArrayList<String> names = getArguments().getStringArrayList(INTENT_OPTIONS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, names);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
if (mListener != null){
mListener.onSelectedElement(which);
}
}
};

return new AlertDialog.Builder(getActivity())
.setTitle(R.string.loginActivity_selectVACenter)
.setCancelable(true)
.setAdapter(adapter, listener)
.create();
}

在实现 appcompat v7 库以合并 Material 特征之前,我用以下句子选择了 Espresso 中的每个元素:

onView(withText("text of the entry"))
.perform(click());

但是加入这个库后,这句话就不行了,Espresso找不到view,抛出NoMatchingViewException。

还有其他方法可以访问对话框选项吗?

最佳答案

我已经找到了这个可能有用的主题:Check if a dialog is displayed with Espresso

在一个可能的答案中有一个 getRootView() 匹配器:

To answer question 4, which the accepted answer does not, I modified the following code, which I found here on Stack Overflow (link) for testing whether a Toast was displayed.

@NonNull
public static ViewInteraction getRootView(@NonNull Activity activity, @IdRes int id) {
return onView(withId(id)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
}

The id passed in is the id of a View currently displayed in your dialog. You could also write the method like so:

@NonNull
public static ViewInteraction getRootView(@NonNull Activity activity, @NonNull String text) {
return onView(withText(text)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
}

And now it's looking for a View containing a particular text string.

Use it like so:

getRootView(getActivity(), R.id.text_id).perform(click());

首先尝试这个。同样在同一主题中,您会发现此方法:

onView(withText(R.string.my_title)).inRoot(isDialog()).check(matches(isDisplayed()));

如果它仍然不起作用,则可能会使用 onData 匹配器。

Whenever you have a ListView, GridView, Spinner, and other Adapter based views, you’ll have to use onData() in order to interact with an item from that list. onData() is targeting directly the data provided by your adapter. What does this mean, we will see in a moment.[1]

你可以在这里阅读:

Get started with a simple test using onData

[codepath] UI Testing with Espresso

Android Espresso onData with doesNotExist

关于android - 使用 ArrayAdapter 和 Material v7 支持库在 Espresso 中选择对话框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34223796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com