Programmatically Perform Child View Click in ExpandableListView - java

I have an ExpandableListView implementation in my application and I am writing unit tests for it. I'm wondering how I can programmatically perform a click on the child view of an expandable header?
This is NOT a question about how to attach onChildClickListener() to the view. I've found many questions regarding that topic, but I already have that implemented and need to test the functionality of that code when the child view is clicked. I know that I can use the performClick() method to click on the header view to expand/collapse the contents, but I need to perform a click on the sublist of a header.

You can try with:
listView.setSelection(position);
but you have to consider this:
If in touch mode, the item will not be selected but it will still be
positioned appropriately. If the specified selection position is less
than 0, then the item at position 0 will be selected.
For more information you can visit the doc page.
I hope it helps.
Edit
As #RyanM appointed, setSelection just change the selection, but it doesn't perform the "click" action. You can perform the action through the function:
listView.performItemClick(goalListView, itemPosition, itemId);
You can find this function in the class AdapterView.
Sorry for the previous mistake to your answer.

Related

Multiselect Recyclerview List

Good day all,
I have a list of items that i want the user to be able select say 5 out of the 10 items and with all 5 be able to send some data off to a webservice.
i found This and This
but neither of them worked or the GitHub Repo is non existent (the second one).
Does anyone have any other tuts on how to do this?
Thank you
What is going on with your proplem?
Is the video (https://www.youtube.com/watch?v=7du30yAL6rI) as your expectation?
In this example, I create an empty class and simple recyclerview.
The view item will handle user click behaviour. If user selected item the click again the item would not select and otherwise. When this action is happening, we have a listener to send to MainActivity (implemented onItemChangeListener a method of interface that is defined in adapter)
In the MainActivity, I create a layout with recyclerView, delete button, and no items labels. The delete button will process delete and update the list data.
The source code is at: https://github.com/liemvo/Example_RecycleMultiSelected

Highlight and disable Listview items

I have a little Problem with my ListView.
I fill my ListView with an ArrayList<String> per ArrayAdapter<String> with more than 100 items.
Before my dialog will be show i want to highlight and disable some items. I have found the solution with ListView.post(new Runnable() {...});
I highlight my items with lv.getChildAt(2).setBackgroundColor(Color.BLUE);
and disbable an item with lv.getChildAt(3).setEnabled(false);
I do both also in an OnItemClickListener().
Now my problem:
if in the ListView are 11 items visible at runtime, the the highligt- and disable-pattern will repeat every 11 items.
i.e. if i highlight just the 3. item also the 14., 25. ... item will get a blue background.
And if i disable the 4. item also the 15. and 26. and so on is disabled.
If i scroll fast to buttom and back to top other items are highlighted and disabled.
Another problem is, that i can only access the first (11) visible items in the post-runnable. If i try to highlight the 20. item the app will crash with a NullPointerException.
What can i do to prevent the "item-recycling" and to get full access to all items before the Dialog is shown?
I'm not sure i understand what you want to achieve but here are some suggestions for you.
1) Always Recycle, you should never avoid recicling since you maybe run into another problem, you will run out of memory.
2) In your model implement the checkeable interface, so the model should know if an item is selected or not, Not the view only
3) When iterating each element make use of a ViewHolder and then check the model to see if the elment being inflaed it selected or not and use the desired color
Please have a look at this example link, it describes the use of CAB (Contextual Action Bar) but it uses the things i'm mentioning.
I hope it helps you.

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'.

Problems with android-pulltorefresh widget

I am having a bit of difficulty implementing the android-pulltorefresh widget by Johan Nilsson found at https://github.com/johannilsson/android-pulltorefresh
The problem I am having is after putting the custom listview into my application everything is fine it, but it asks to Tap to Refresh the list view but I need it to be set to pull down to refresh.
The code I am using below is pretty much from the github page and a screenshot of the app can be found below do demonstrate my issue:
PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listView);
lv.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
GetData getData = new GetData();
getData.execute();
}
I need the Tap to refresh header gone and only to be shown once the listview has been dragged down. I get the feeling I just need to change some sort of flag but I can't find where this would be.
Unfortunately there is no way to work around this. The entire control is built around the idea that the "Pull to Refresh" header is a normal listview item that gets hidden by scrolling the list upward. Unfortunately, when you have a very short list, the list cannot be scrolled upward to hide the first item cause there are not enough items in the list -- so the fallback is to show the first item (the header) as well and have it display "Tap to Refresh".
EDIT: One kludge you may be able to do is insert dummy blank items so the list has enough items to hide the top header list item.
use this code
Hope it works for you.

Updating the UI during a after a button push

I have been thinking about adding a button to a View which when pressed adds another view on the bottom. It would resemble the Clock application that comes preinstalled in which you push a view in form of an alarm which is then added to a list.
Does anyone have any experience with how to implement that?
Have a look at the documentation for ListView, particularly the setAdapter() method. You'll want to have some sort of backing collection most likely as the adapter, such as an ArrayAdapter, to which you can add new items, and have them populate into the list.
Use a listview, populate it.
Then when you press the button, add something to your data, then:
public void notifyDataSetChanged ()
Since: API Level 1 Notifies the attached observers that the underlying
data has been changed and any View reflecting the data set should
refresh itself.

Categories