I would like to create a listview that allows multiple choice. The typical solutions is to get a cursor and use the SimpleCursorAdapter.
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
I am able to get this working when using the R.layout.simple_list_item_multiple_choice. I get the checkmarks to work when multiple items are selected.
So I decided to try it with a custom made layout. Here is the XML code for my layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
</LinearLayout>
So here is my issue. The layout is inflated fine using the same code and setting the ChoiceMode to multiple on my listview. The data from the cursor is populated fine.
However, the issue I have is that the checkmarks do not show up as selected (a check in the box) when I click on the item. Is there something I am missing that would not involve creating a custom adapter?
l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
l2 is my listview.
I'm not up on CheckedTextView... from my understanding, it should toggle the check when you click the line.
I suppose you could try and force it like this:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1);
chkTxt.toggle();
}
Note this would be a hack and you should really find the root problem, but it might get you going in the meantime.
EDIT
After much googling, I found the issue... the root of the row layout needs to be checkable to use CheckedTextView, and a LinearLayout is not.
More googling found a solution... override the LinearLayout class.
Instructions/code here
The problem with "the issue I have is that the checkmarks do not show up as selected" is because of your wrong layout. It should be without LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
Then the SimpleCursorAdapter can set properly your "checkedTextView1" (with visible effect).
In my case I used the ArrayAdapter and it worked.
list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
R.id.category_checkedText, this.categories));
Related
I'm trying to add a ListView that has an ImageButton in it. Everything works except my onItemClick() isn't called. I've googled around and searched on StackOverflow to find suggestions of
android:descendantFocusability="blocksDescendants"
and
android:focusable="false"
I've tried both of these but neither work. This my layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/listview_item_height"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_title_text_size"
android:textColor="#color/text_primary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
<TextView
android:id="#+id/song_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_subtitle_text_size"
android:textColor="#color/text_secondary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
</LinearLayout>
<TextView
android:id="#+id/song_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<ImageButton
android:id="#+id/overflow_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:background="?android:selectableItemBackground"
android:src="#drawable/ic_action_overflow"
android:scaleX="0.75"
android:scaleY="0.75" />
</LinearLayout>
Then my onItemClick() simply logs the id of the element clicked.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
long id = adapter.getItemId(position);
Log.d(TAG, id + "");
}
I never get the output in the console though. I do get the selectable background when I click on the item though. I did not get that until adding "blocksDescendants" to the code. Any help is appreciated as I've spent a bit of time on this already.
You can use imagsView instead of imageButton and do onClick actions when list item is clicked
Or
You set onClickListener after layout is inflated in view adapter.
Read this: http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html?m=1
Also this should be set like this to let system listen for clicks:
ListView focusable
android:focusable="true"
Button not focusable
android:focusable="false"
Or
You can use this in xml. On click, it will look for your method in same activity. Doesn't work for fragments.
android:onClick="myMethod"
Have you tried using an ImageView instead? That way by default it won't intercept your clicks.
Or you could try playing with onInterceptTouchEvent.
http://developer.android.com/training/gestures/viewgroup.html
I have a small Android APP with a listview in it. Sometimes the listview does not react on my inputs. I can see that I touched on one item (it changes the color as I touched it), but the program does nothing.
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false" >
<TextView
android:id="#+id/textViewRowLable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textSize="50sp" />
<TextView
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/textViewRowLable"
android:gravity="right"
android:textSize="25sp" />
<TextView
android:id="#+id/textViewRemainingTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/textViewTime"
android:layout_toRightOf="#id/textViewRowLable"
android:gravity="right"
android:textSize="25sp" />
</RelativeLayout>
This is the listner:
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
}
});
Could someone give me a hint why it is not always working?
Thank you
Try using android:descendantFocusability="blocksDescendants" on your RelativeLayout
Also go through this tutorials:
Listview tips and tricks
I had a similar problem. Than I looked at the Android source and found the reason.
In the click handling, there is the following line
if (mDataChanged) return;
So the click is discarded, when the internal list data has changed.
I have updated the list very often. Reducing the update interval solved the problem nearly (after the change, only 1 of 100 clicks was not recognized)
I had set up an Activity and a Fragment, and in the layout I had a ListView, which I gave an arbitrary name and accessed via findViewById(). All was working fine, until I noticed that scrolling didn't work for the ListView.
There are many threads here on SO about ListView and lack of scrolling. I used much more time than I care to admit trying out one possible solution after another. Nothing worked, until I stumbled upon the solution, which I'll describe in an answer to this question.
EDIT:
I've deleted the answer I posted to my own question, where I claimed that you had to use ListFragment (or SherlockListFragment) to get scrolling of a ListView to work.
Thanks to Zackehh9lives I've determined that I was wrong in that claim.
The key phrase in his answer is that the stump of code he shows is in the onCreateView() method. After some experimenting, I think the situation is as follows:
If you use ListFragment (or SherlockListFragment) you have to set up the ListView widget in the onActivityCreated() method, and ListView scrolling works.
If you use Fragment (or SherlockFragment) you can set up ListView in onActivityCreated() or in onCreateView(), but if you do it in onActivityCreated() then scrolling doesn't work.
That was my problem - I was using SherlockFragment and setting up ListView in onActivityCreated().
I have a ListView in a SherlockFragment and it scrolls just fine. Not sure what the issue is? Perhaps you just have a something incorrect somewhere (might want to post your code?). Below is my ListView and XML:
MyFragment.java:
// Find the ListView we're using
list = (ListView)view.findViewById(R.id.listView);
// Set the vertical edges to fade when scrolling
list.setVerticalFadingEdgeEnabled(true);
// Create a new adapter
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.title, questions);
// Set the adapter to the list
list.setAdapter(adapter);
// Set on item click listener to the ListView
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Do stuff
}
});
All that code is all I have related to my ListView in the Fragment, and it's in my onCreateView(). Below is the XML for it.
ListItem.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/arrow"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/title"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title"
android:layout_alignParentBottom="true"
android:layout_below="#+id/title"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="#string/date"
android:textColor="#7F7F7F"
android:textSize="12sp" />
</RelativeLayout>
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="#string/app_name"
android:src="#drawable/arrow_next" />
ListView.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:paddingTop="-2dp" >
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
If you post your code, we have a better chance at helping :)
As a new android programmer I have my obligatory todo app running. It is acceptable for Christmas shopping, but I'd really like to make the whole list-item row clickable. Right now I have to tap the text. The shorter the item the harder to hit.
My main activity extends ListActivity. I use a subclass of ResourceCursorAdapter for the list itself. My layout looks like this (thanks!):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/todo_row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox android:text=""
android:id="#+id/todo_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:checked="false"
></CheckBox>
<TextView android:text=""
android:id="#+id/todo_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>
Tapping anywhere in the row is the default behavior of a listView. How are you setting up your onClick listeners? You should use the onItemClickListener.
I've been implementing a number of custom-lists, all worked fine. Now I'm trying to add an EditText to the list's rows. The rows look like that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/tv_quest_listrow_categorical_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blabla"
android:paddingLeft="10dp"
android:minHeight="48dp"
android:gravity="center_vertical" />
<EditText android:id="#+id/tv_quest_listrow_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:gravity="right|center_vertical"
android:singleLine="true"
android:enabled="false"
android:imeOptions="actionDone" />
</RelativeLayout>
When constructing the list I set an ItemClickListener for the list with listView.setOnItemClickListener(onItemClick);. This used to work fine but fails as soon as there is an EditText in the list rows.
Does anyone have a hint on how to do this?
Regards,
Steff
I found this page, which has some good information on it. Apparently others have had this issue as well.
Try iterating through the items of the ListView and set the OnItemClick listner individually for each item
thanks