I create a custom listview.
I want to select any item using another button click or something views click.
Already I tried via these code for custom selection.
And I also want to get previous selected item on resume.
listView.setItemChecked(2,true);
listView.setSelection(2);
listView.requestFocus();
But everytime , I failed.
I used this method to create custom listview.
Android ListView with Custom Adapter Example Tutorial
Advance Thanks .
For setItemChecked to work, your list items need to be Checkable; you can, for example use CheckedTextView (at the top level of the item view, no further layout around it).
For setSelection to work, you need to use it inside a Runnable, like so:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(2);
}
});
In both cases, don't ask why.
For a selection to become visible, you can put a statelist in the background attribute of your list item.
Related
My question is simple: How do I use long click to let the users select items from the ListView ? So far, I only know how to detect 'short' clicks and take appropriate actions.
Also, I would like to display a check mark on the selected item. How can that be done ?
Simple : OnLongClickListener
Then you need to manually remember what is selected or not. You need to notify the list from the changes and do something in the getView method of you adapter.
It would be a good practice to use the Contextual ActionBar mode to interact with all item at once, see here.
Answered in https://stackoverflow.com/questions/12090394/i-cant-get-longclick-to-work-on-listactivity :
// Optional, added if done from ListActivity (or possibly ListFragment)
ListView lv = getListView();
// Set on this if overriding OnItemLongClickListener, otherwise use an anonymous inner function
lv.setOnItemLongClickListener(this);
It works the same way as the onClickListener, just instead you're checking the onLongClickListener. So you'd have this kind of structure:
your_view.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
...
}
});
If you want to display a check mark, in my opinion the best way is defining your own row layout, where you just define a CheckBox at the right side the content of the row. This way, instead of passing the ArrayAdapter some Android layout, you'd specify your new layout, something like:
your_adapter = new ArrayAdapter(context, R.layout.your_new_layout, initial_rows);
I am working on a simple to-do list app, and on the main view I have a list of tasks with a checkmark aside to each, as the image below:
Upon clicking on the checkmark I want the respective task to be removed from the list. I tried to implement this as TextViews (for the tasks) and Buttons (for the checkmark), but I would need to know the number/position of the clicked checkmark (0,1,2 or 3) to remove the correct task from my array. Can I get this some how?
I also thought about implementing the tasks/checkmarks as a ListView, but then I would need to set a onItemClickListener only on the checkmark, and not on the task text. Is this possible?
Any other ideas? Thanks.
Checkbox has a tag property which you can use to set custom data like row number. Something like this should work,
checkbox.setTag(row_number);
So when you click on it, do something like
int rowNum = Integer.parseInt(checkbox.getTag());
removeTaskAt(rowNum);
If you're using a Custom Adapter, you can add a click listener on the check on getView, so the click is only on the checkbox and not on the full item.
The only problem with this is that the scroll may not work if you try to scroll by clicking on the checkbox.
i am trying to change the background color of the selected row in a listview and i am able to do it.
But when i am clicking on another row then the background color of the previously selectedrow remains unchanged. I have the position of the previously selected row, Can anybody help me that how can change the background color of previously selected row as it was before??
If you keep track and update the listItems clicked state in your model you can just put the code for displaying has been clicked-color in the adapter and then call
adapter.notifyDatasetChanged();
I think this might be easier if you look at it another way.
Currently, your logic is "if I click on this row, change its color to a special color and change the old row's color back to the original color". However, this doesn't seem to be the logic you actually want. Rather, you want the last clicked (aka selected) row to be a different color.
You haven't posted any code, so I don't know if you are implementing your own ListAdapter in this project. That is the approach I would take. Create a class which extends ListAdapter, and create an additional private variable that stores the position of the last selected row. Then in the overriden getView() method, do a quick check
if(rowPosition == lastSelectedRowPosition)
viewToReturn.setBackgroundColor();
If you aren't sure how to make your own list adapter, check out the tutorial at http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx.
In your activity use setOnItemClickListener of Your_List Object.
See Demo Code:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE); // <--- Use color you like here
// ^ this v gives current row.
}
});
This will make background color changed for that row forever.
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.
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.