简体   繁体   English

在自定义列表视图中获取视图

[英]get views in a custom listview

I am using robotium to test my project. 我正在使用Robotium来测试我的项目。 I am using custom listviews in my project. 我在项目中使用自定义列表视图。 There will be minimum 3 listviews in a page which resides in a view pager. 驻留在视图分页器中的页面中至少有3个列表视图。 My custom listview name is MyDragNDropList . 我的自定义列表视图名称为MyDragNDropList In this listviews there will be 1 button in each row. 在此列表视图中,每行将有1个按钮。 This button is to add that item to my personalized list. 此按钮是将该项目添加到我的个性化列表中。 Once the item is added the button will be disabled. 添加项目后,该按钮将被禁用。 Initially i was using 最初我是用

  solo.clickOnText("button text");  or
  solo.clickOnButton("button text"); or
  solo.clickOnButton(buttonindex);

but this is not working now. 但这现在不起作用。 So i have tried another method. 所以我尝试了另一种方法。 I am setting the listview to a listview object created for unit test project. 我将listview设置为为单元测试项目创建的listview对象。 Then 然后

        solo.scrollListToLine(2, position); 
        solo.waitForDialogToClose(1000);
        ListView myList=UnitTestHelperClass.getInstance().listView;
        View listElement = myList.getChildAt(position);
        View btn=listElement.findViewById(com.safeway.client.android.R.id.list_button);
        solo.clickOnView(btn);

If the first visible item's button is enabled then this code will work. 如果启用了第一个可见项目的按钮,则此代码将起作用。 But if the lst is scrolled then i am getting NullPointerException in below line. 但是,如果第一个滚动,则我在下面的行中获取NullPointerException。

 View btn=listElement.findViewById(com.safeway.client.android.R.id.list_button);

Why is it so? 为什么会这样呢? How can i resolve this issue? 我该如何解决这个问题? Please help me. 请帮我。

EDIT I have tried with another method. 编辑我尝试了另一种方法。 Instead of setting listview from source code i am getting this in the test project itself. 我没有在源代码中设置listview,而是在测试项目本身中获得了它。

            solo.scrollListToLine(2, position);
        ListView list=solo.getCurrentViews(ListView.class).get(2);
        View listElement=list.getChildAt(position);
        View btn=listElement.findViewById(com.safeway.client.android.R.id.add_offer_button);
        solo.clickOnView(btn);

But here also i am getting the same issue.First two items button click is working fine.but for third item i am getting null pointer exception. 但是在这里我也遇到了同样的问题。单击前两个项目的按钮工作正常。但是对于第三个项目,我得到了空指针异常。

I got answer:) 我得到了答案:)

public View getViewAtIndex(final ListView listElement, final int indexInList, Instrumentation instrumentation) {
ListView parent = listElement;
if (parent != null) {
    if (indexInList <= parent.getAdapter().getCount()) {
        scrollListTo(parent, indexInList, instrumentation);
        int indexToUse = indexInList - parent.getFirstVisiblePosition();
        return parent.getChildAt(indexToUse);
    }
}
return null;
 }


public <T extends AbsListView> void scrollListTo(final T listView,
    final int index, Instrumentation instrumentation) {
instrumentation.runOnMainSync(new Runnable() {
    @Override
    public void run() {
        listView.setSelection(index);
    }
});
instrumentation.waitForIdleSync();
}

If you dont have too much items and you want load latest configuration... here is my example whoch saves the amount of selected star. 如果您没有太多项目,并且想加载最新配置...这是我的示例,witch可以节省所选星标的数量。 It's just an example. 这只是一个例子。 It's just to show you how to save some information in adapter and load adfter scroll. 只是向您展示如何在适配器中保存一些信息并加载adfter滚动条。

public class CustomAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private final String[] elements;
    private final int[] stars;
    private CustomClickListener listener;

    public CustomAdapter(Activity activity) {
        this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.elements = activity.getResources().getStringArray(R.array.questions);

        stars = new int[elements.length];
        resetStars(stars);

        listener = new CustomClickListener(stars, activity);

    }

    private void resetStars(int[] stars) {
        for(int i=0;i<stars.length;i++)
            stars[i] = 1;
    }

    @Override
    public int getCount() {
        return elements.length;
    }

    @Override
    public String getItem(int i) {
        return elements[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.item, null);

        TextView text = (TextView) view.findViewById(R.id.question);
        ColoredRatingBar ratingBar = (ColoredRatingBar) view.findViewById(R.id.ratingbar);

        try {
            Log.d("xxx", "setting stars for " + position);
            ratingBar.setRating(stars[position]);
        } catch (Exception e) {
            Log.d("xxx", "exception ignored");
        }

        ratingBar.setOnRatingBarChangeListener(new ColoredRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(ColoredRatingBar ratingBar, float rating, boolean fromUser) {
                stars[position] = (int) rating;
            }
        });

        View submit = view.findViewById(R.id.item_submit);
        Button submitButton = (Button) view.findViewById(R.id.submitButton);

        text.setText(getItem(position));

        if (position == (getCount() - 1)) {
            submit.setVisibility(View.VISIBLE);

            submitButton.setOnClickListener(listener);
        }

        return view;

    }



}

Sorry that I've not implemented ViewHolder class :( 抱歉,我没有实现ViewHolder类:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM