Android ListView - onListItemClick does not work properly - java

I created a ListView in Android, and a corresponding ListActivity. Each individual item in the ListView has just one TextView (I plan to add an image and a CheckBox later).The ListActivity overrides the onListItemClick to perform certain tasks on click of any item on the list.
Heres whats happening -
When I first tried clicking on any item, nothing happened.
I then tried setting the properties "Focusable" and "Focusable in Touch Mode" to false for the TextView, as mentioned here, here and here. The List items started recognizing clicks, but only when I clicked somewhere away from the TextView. Whenever I tried clicking on the TextView or anywhere near it, it did not work.
I also tried changing various attributes like Clickable, but nothing has worked so far.
Any idea what I could be doing wrong ?
Thanks

After playing around with virtually every attribute in my TextView, I finally found the reason why it was not working. It was because of the attribute android:inputType="text" in my TextView. I'm not sure why I added that piece of code (I probably copied the TextView from one of my other applications), but removing it solves my problem.

Class which will listen clicks on ListView should implement interface AdapterView.OnItemClickListener

Related

Clicking on one item changes the background of another item in recycler view

I don't know how to fix it. Below is the video link:
https://www.facebook.com/groups/2297396350278679/permalink/5183111161707169/?app=fbl
i dont know much about recycleview but i am facing the same problem in the listview,
its happend if you make the background changed for example if(_position==1),
to fix it in simple way dont make the background changed onbind and change it onclick and to change it when the content refreshed you can use .setSelection method.

ListView inside ListView item

I want to ask one question, should I use ListView inside ListView item? Or I should redesign and remake my idea?
Example of ListView, but should be with couple sub items:
A ListView inside a ListView item is never a good idea. If you want sublists inside your list you can, for example, use a ExpandableListView. However, with the migration from ListViews to RecyclerViews I would recommend you go and implement this instead.
A ListView will never work correctly embedded as an item in another ListView. In fact, scrolling things inside other scrolling things is almost never what you really want to do, as it would be confusing to the user how to actually navigate your screen.

Markable ListView with changing Actionbar Actions

Im trying to find out how I can mark things in ListViews with a longClick. After I selected one item ,the Actionbar should change with diffrent options (buttons like delete, add , copy etc) which Im able to execute.I really didnt know how I can find these examples because Its a kind unique I guess. I founded threads where I can Mark an Item which gets a diffrent color and nothing else. How can I achieve this ?
Before selecting :
After long click :
Well you can add an onLongClickListener in your ListView or RecyclerView but for each list_item you will have to specify a checkbox that will become visible only when you longClick the ListView.
Then if you do not want to mess up the actionBar with new menu items you can create a context menu that will do your job.
You can use a ListPopupWindow or you can also use a CardView with a contexual menu as shown in the picture here.

Clicking on a ListView

I have a ListView with a header and a footer. I added two items with a class of my own extending the ArrayAdapter class. In this extended class, I have overridden the getView function because I want Views which are not TextViews to appear in my ListView.
On this ListView, I have set an onClickListener.
The problem is that this onClickListener is started when I click either on the header or on the footer, but never when I click on my items.
Of course, the View returned by getView is set to be clickable.
What am I missing?
Do you want to do something different when each list element is clicked? If so, you should programmatically set an onClickListener for each of those 2 elements added. Can you post your code?
It sounds like you are making this much more complicated than necessary. You can create your own layout for each row of a ListView using an XML file. This allows you to use any combinations of Views that you wish. To control what happens when the user clicks on a row in the ListView, your activity class should extend ListActivity and override onItemClickListener(). Alternatively, you can call setOnItemClickListener() on your ListView. For more details, I suggest that you read the Android ListView Developer Guide.
The solution I found : the click listener attached to the 'ListView' is only used for the clicks on the header and the footer. For my custom rows, in my ArrayAdapter's 'getView' function, I attached one listener to the 'TextView' and another to the 'ImageButton'.

Checkbox in ListView with odd checking behaviour

I am new to Java and Andriod development.
I have a view within my app which contains a ListView
I then have a custom Adapter which inherits from an ArrayAdapter
within the view in the getView method I return a row which contains some text and a checkbox
now so far this all works great my list is populated and I can check my items, and all the events seem to fire as expected.
My problem that I am having is that I check the first 3 items and then I notice that the 11th, 12th and 13th items are checked and as I scroll I see that at regular intervals other checkboxes also seem to be checked in the same pattern.
If I check about 10 checkboxes then it will end up checking all the items in the list of about 80...
can anyone explain what I have done wrong?
I dont think any code will help explain this as I do not set the checkstate of the checkbox anywhere, this is all handled itself, so the fact items are being checked is puzzling me.
Thanks in advance
This is happening because Android recycles list items for performance purposes, here you have the solution of this problem:
ListView reusing views when ... I don't want it to
Hope to help :)
This is expected behavior. ListView reuses views in the adapter.
So what you want is to keep a Set of "selected items" and add an item or remove it when the CheckBox is clicked and then in your getView add the following code:
if(mSelected.contains(getItem(position)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
This way when the row is reused the CheckBox will be the proper status.

Categories