Preselect items in listView Android - android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.

Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

Related

Changing the font colour of one item in a ListView?

The title pretty much says it all. I've got a ListView I'm using in my navigation bar/side menu and want to change the colour of the first item. I can't seem to get a hold of the view itself to setTextColor() or whatever the method would be, so how else can I do this?
You want to change the colour of the textview (or whatever view) in your adapter by overriding or implementing getView().
I assume you don't have a custom adapter or you wouldn't be asking this question, so you might want to check out some tutorials on how to use a custom adapter to create a custom appearance for your list items.

Overriding Android ListView onItemClick causing item to be selected (checked)

I am trying to better understand the internal functioning of ListView as it pertains to selecting one or multiple items- it's actually amazing how difficult it was for me to even get this far in my understanding.
By default a regular click on a ListView item is setting the 'checked' state for that item to true. How do I override this behavior so this selection does not happen?
And more fundamentally, what are the underlying ListView mechanics here? Is the row view's default onClick then calling the ListView's onItemClick/LongClick handlers, or how does this click handling get sequenced?
I do want to allow a choiceMode of multipleChoice, but I only want to select it onItemLongClick. Overriding onItemClick does not change this behavior, and overriding the row view's onClick handler in the adapter getView() function seems to prevent the ListView onItemClick and onItemLongClick from ever happening.
Below is more detailed context on my application
My goal is to have my ListActivity display a ListView, which functions as follows:
Clicking an item performs a non-selecting action (expands the row to show more info)
Long clicking an item selects it. Selecting an item is indicated by highlighting the background of the row (as in the Gmail app)
You can select multiple items
My application structure is:
Activity is an extension of ListActivity
Adapter is an extension of ArrayAdapter<>
ListView row layouts are completely custom layouts (not any sort of built-in ListView row layout)
My understanding of the built-in functionality for ListView has me to the point where I am
setting choiceMode to multipleChoice
using the ListView 'checked' functionality for making and tracking the selections
using a custom selector as an 'activatedBackgroundIndicator' to show the highlighting (example here)
Keep an ArrayList to maintain ListView items selected position. When a ListView item is selected check in that ArrayList whether item position is in ArrayList or not. If item is not in ArrayList change state of Item to checked else change state to unchecked and remove the position object from ArrayList. This worked for me.

ListView Item and OnClick problem

When you touch some grid or listview items in an android app, he change the background color telling you that you have pressed it.
I'm having a problem in my app. When I create that a listview for instance, and I press it in my phone, he change color, but if I set the OnClick property, the item dont change it color. Whats wrong? The OnClick works as its suppose.
Thanks
Generally with ListView you want to use an OnItemClickListener.
This gives you a chance to perform an action based on the position in the list that was clicked.
If you need access to the view adapter from within onItemClick then you can use AdapterView#getAdapter() on the AdapterView that is supplied to you.

android listview

I have created one list view.. it is having 5 items...
Now I want split the list items...
when user clickon the first listitem or focus on first item then immediately it has to show followed some text views or other things..but it has to show same list..
and agian same when he clickon or focus on the second item that first item has to be close and second item has to act some thing....
I think you need to implement the concept of "Expandable Listview", so that the clicking on one item, it will be expanded with their sub-items.
Refer the android-sdk page of Expandable ListView: http://developer.android.com/reference/android/widget/ExpandableListView.html
For having an example, check this site: http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html
Pls, check the below image, do you want to perform as same ????
If you want to do the same, it is already given in the "API-Demos" at Views/Expandable Lists/1. Custom Adapter.
Enjoy !!
The problem is that you cannot use the standard ListView in your case, because in the standard ListView, the View of each row has to be one TextView.
In your case, you need it to be at least two TextViews (The standard Text, and the one that's gonna show up onClick/onFocus).
You have to create your custom ListAdapter, and override the getView() function.
Here is a code snippet that shows how to do it properly:
Custom Adapter
In the getView(), you have to inflate the XML file that describes your List Row, and return it.
In your case, I believe your XML file should contain 2 TextViews, one visible and one invisible.
Then, to handle the clicks, you can set a onItemClickListener on your ListView in your Activity class.
The best way may be to have your Activity class implementing onItemClickListener, and to use this onItemClickListener to handle those.
In the onClick() function, you just have to set the Visibility of your hidden TextView to VISIBLE.
You need to build custom rows, and handle showing more text on each row, there is no easy magicall way of doing it, but inflating your own rows, and setting a couple of attributes visibility isnt all that hard either.

Android: How to work with Checked ListViews

I wish to work with checked list views wherein only one item can be selected at a time. Some queries related to this:
Is it advised to work with CheckedTextView as the ListView items, or a combination of CheckBox and TextView?
If using CheckedTextView, the text comes first and the checkbox appears on the right edge. Is it possible to make the checkbox appear on the left of the TextView?
How can I make one of the items as checked in onCreate()?
Note: I am using array adapter and calling setAdapter() to populate list.
You need to extend ArrayAdapter and use LayoutInflater to inflate the row layout as you need. This way you have full flexibility in list creation.
Please check this example, where basic idea is described:
Custom list view

Categories

Resources