I have a problem with items in this list. The normal behavior is that when a user clicks on one element, the background color changes to orange. This does not happen with my application, and I cannot understand why.
The main.xml file has only:
<android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" />
The xml snippet only has:
<ListView android:id="@android:id/list" style="@style/MyListView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView>
The view that I am inflating inside the listView element:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textViewName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/imageView1" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignLeft="@+id/textViewName" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="10dp" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/bottom_arrow" /> </RelativeLayout>
The style I defined for ListView:
<style name="MyListView" parent="@android:style/Widget.ListView.White"> <item name="android:listSelector">@drawable/list_subject_selector</item> <item name="android:cacheColorHint">@android:color/transparent</item> <item name="android:background">@drawable/list_subject_selector</item> </style>
And the list_subject_selector output list:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/list_subject_focused" android:state_pressed="true"/> <item android:drawable="@drawable/list_subject_unfocused" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> </selector>
I have no idea why this is happening ... I am testing Android ICS.
Any ideas?
Thanks to everyone, Felipe
UPDATE 1 This is the ListFragment code:
public class MyFragment extends ListFragment { private ListAdaptor adapter; private InternalDB db; Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = getActivity(); db = new InternalDB(getActivity()); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayList<EventVO> listVO = db.getSummaryEvents(true); adapter = new ListAdaptor(mContext, R.layout.event_inflate, listVO); setListAdapter(adapter); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout rl = (LinearLayout) inflater.inflate(R.layout.general_list, container, false); return rl; } private class ListAdaptor extends ArrayAdapter<EventVO> { private ArrayList<EventVO> listVO; public ListAdaptor(Context context, int textViewResourceId, ArrayList<EventVO> items) { super(context, textViewResourceId, items); this.listVO = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { v = ViewGroup.inflate(getContext(), R.layout.event_inflate, null); } final EventVO o = listVO.get(position); TextView fullName = (TextView) v.findViewById(R.id.textViewName); fullName.setText(o.getUserName());
UPDATE 2 Now it works! It was very unpleasant. I did this to make it work:
The xml file I used to inflate had:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" >
I changed to:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
But, I think it really changed that: Inside onActivityCreated ()
list = getListView(); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ListAdaptor la = (ListAdaptor)arg0.getAdapter(); EventVO vo = la.getItem(arg2); Intent i = new Intent(mContext, EventDetailsActivity.class); i.putExtra("USER_ID", vo.getUserId()); i.putExtra("IS_BORROWED", true); startActivity(i); }
The list of strings is important = getListView (); I did not get ListView through this piece of code. I was getting listView in onCreateView () method with findViewById. I believe that I have lost touch with the actual ListView object.
Thanks to everyone who helped me! :)
Felipe});