get views in a custom listview

871 Views Asked by At

I am using robotium to test my project. I am using custom listviews in my project. There will be minimum 3 listviews in a page which resides in a view pager. My custom listview name is MyDragNDropList. In this listviews there will be 1 button in each row. 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. 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.

 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.

            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.

2

There are 2 best solutions below

0
On BEST ANSWER

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();
}
3
On

If you dont have too much items and you want load latest configuration... here is my example whoch saves the amount of selected star. It's just an example. It's just to show you how to save some information in adapter and load adfter scroll.

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 :(