I have a custom cursor adapter I'm using to fill a ListView. The inflater has 2 CheckBoxes which both have onclick handlers. I want to be able to check the state of the sibling CheckBox when one is touched.
Does anyone have experience with something similar? I'm not sure how to grab the ListView row of the CheckBox and the cursor adapter is always in the state of the last row added.
You could probably call getParent() on the clicked view, and then findViewById on the parent to navigate back down to the other checkbox. Of course you might have to go up a couple levels if the checkboxes are wrapped in other views.
Related
I have a View in a custom layout of a ListView, now I want to assign an onClickListener to this View (in a Fragment). But if I try
getActivity().findViewById(R.id.rowleft).setOnClickListener( ...
it throws a NullPointerException. Why is this? In the layout file the View has the ID "rowleft" Does the id change when it is in a ListView? Or how do I see which row triggered the event?
What I want is two Views in one Item to know if the user clicked on the left or the right side.
Thank you
Set up your OnClickListeners in your adapter getView() where you're setting up the listview item view anyway.
You can override getView() in an ArrayAdapter. Call super.getView() to get the view to call findViewById() on for the purpose of setting click listeners.
Don't call findViewById() on the activity. Even when the listview items are part of the activity hierarchy, it's not really practical to find views with the same id from different listview items.
ListView.getChildAt(x).findViewById(R.id.rowleft).setOnClickListener( ...
I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}
I reuse the same ListView for navigating through data, while updating the ListView data based on the user clicking a row. Up until now I had been using the same row View throughout the activity, but after the user clicks a row, I would like to change to a different row layout for all of the ListView's rows.
To be more concrete, say the ListView contains rows using layout1, and the user clicks on a row. The ListView's array adapter subsequently gets a new set of data and all the rows are now using layout2.
Rather than create a new ListView after the click or mess with setTag ugliness, I'd just like to clear all of the the cached row views that are sent via getView's convertView. I still want to subsequently recycle the layout2 views.
Can I do this, or do I have to "bite the bullet?"
It looks like ListView.invalidateViews() will do this:
public void invalidateViews ()
Causes all the views to be rebuilt and redrawn.
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
I have a ListView in my android activity. And I populate the ListView by sub-class the BaseAdaptor (which returns a View in getView() method).
What if in my click listener of a button in a list item view, I
change the text of the TextView in the List item view
or
change the dimension of the list item view by adding/removing children of the list item view
What is an efficient to refresh my listView? I don't want the listView to re-trigger a query since there is no data change.
Thank you.
if you just change the content of your Children View, you have not more to do than TextView.setText(newText).
If you change the count of your ListView childrens, you have to call BaseAdaptor.notifyDataSetChanged()
I was about to suggest that you invalidate(), but you don't want to trigger a redraw, so I can only suggest that some form of global flag is your best bet. Set the flag, invalidate or redraw and query (and reset) the flag in OnDraw()