• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.app.ListActivity;
4 import android.view.View;
5 import android.view.ViewGroup;
6 import android.widget.AdapterView;
7 import android.widget.ListAdapter;
8 import android.widget.ListView;
9 import com.xtremelabs.robolectric.internal.Implementation;
10 import com.xtremelabs.robolectric.internal.Implements;
11 
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 
15 /**
16  * Shadow of {@code ListActivity} that supports the retrieval of {@code ListViews}
17  */
18 
19 @SuppressWarnings({"UnusedDeclaration"})
20 @Implements(ListActivity.class)
21 public class ShadowListActivity extends ShadowActivity {
22     private ListView listView;
23     private ListAdapter listAdapter;
24 
25     @Implementation
getListView()26     public ListView getListView() {
27         if (listView == null) {
28             if ((listView = findListView(getContentView())) == null) {
29                 throw new RuntimeException("No ListView found under content view");
30             }
31         }
32         return listView;
33     }
34 
setListView(ListView view)35     public void setListView(ListView view) {
36     	listView = view;
37     }
38 
39     @Implementation
setListAdapter(ListAdapter listAdapter)40     public void setListAdapter(ListAdapter listAdapter) {
41         this.listAdapter = listAdapter;
42         ListView lv = findListView(getContentView());
43         if (lv != null) {
44             lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
45                 @Override
46                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
47                     try {
48                         Method handler = realActivity.getClass().getDeclaredMethod("onListItemClick",
49                                 ListView.class, View.class, int.class, long.class);
50                         handler.setAccessible(true);
51                         handler.invoke(realActivity, parent, view, position, id);
52                     } catch (NoSuchMethodException ignored) {
53                     } catch (InvocationTargetException ignored) {
54                     } catch (IllegalAccessException ignored) {
55                     }
56                 }
57             });
58             lv.setAdapter(listAdapter);
59         }
60     }
61 
62     @Implementation
getListAdapter()63     public ListAdapter getListAdapter() {
64         return listAdapter;
65     }
66 
findListView(View parent)67     private ListView findListView(View parent) {
68         if (parent instanceof ListView) {
69             return (ListView) parent;
70         } else if (parent instanceof ViewGroup) {
71             ViewGroup viewGroup = (ViewGroup) parent;
72             for (int i = 0; i < viewGroup.getChildCount(); i++) {
73                 ListView listView = findListView(viewGroup.getChildAt(i));
74                 if (listView != null) {
75                     return listView;
76                 }
77             }
78         }
79         return null;
80     }
81 }
82