Weird behavior with selectAllOnFocus when EditText is in ListView - java

This problem has been bugging me for a while now. I have a list view and in each row there are three EditText in which user can enter data. When a user selects an edit box, the text should be selected, so when he types the first key, the previous text is removed.
This all works perfectly with selectAllOnFocus option set to true. The problem occurs when I have to call notifyDataSetChanged. There is a button in each row that deletes the row and that is when I call the notifyDataSetChanged method of the adapter. After this, when a user selects an EditBox, it doesn't selects the whole text, but the cursor sets to the left side of the text and an arrow is displayed below the cursor.
I tried setting selectAllOnFocus in code and in layout file, but no luck.
What should I try? Am I missing something?
I tried with disabling the focus changed event, no luck.
Thanks!

What is the expected behavior your trying to achieve?
EDIT:
Why not try firing off a two click events manually. Its a slightly hacky solution but if it works than who cares. What you can do is get the view from your list view that you want to have the text selected in and fire off the two clicks in a post like so
myEdit.post(new Runnable(){
public void run()
{
myEdit.performClick();
myEdit.performClick();
}
}
using the post would make sure that the events got fired after the view had been re-inflated after calling invalidate on your listview adapter

Since this was happening whenever I call adapter.notifyDataSetChanged(), I figured I should try to go around it. And this is what I ended up doing:
adapter.clear();
adapter = new AdapterOfWhateverItemsTypeIs(this, items);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
After this, I got the expected behavior of selectAllOnFocus option of EditText.

Related

Android Studio, change the behavior of the back button to affect items in a RecyclerView

I have a problem regarding the back button feature together with RecyclerView, my goal is to emulate the behavior in the Contacts application, where you press and hold (long press) an item in the RecyclerView and a check box appears behind the item(s) (all of them). Then, when you press the back button, all the check boxes disappear.
Regarding the information I provided we can break the problem in two parts:
First, we need to solve the long click problem, which I suspect we can use
NotifyDataSetChanged() together with hiding the check boxes in the XML so that we can switch between checkbox.setVisibility(VISIBLE) and checkbox.setVisibility(GONE).
Lastly, and this is the hardest part for me, I would like that, when back button is pressed, all the check boxes disappear, instead of leaving the app.
Thank you in advance and sorry if the problem is not understandable, as this is the first time I am posting a problem and English is not my native language.
What you are describing is known as Contextual Action Mode.
You'll notice, that when you long press the item the toolbar (action bar) on top changes and shows the number of selected items and a set of action you can apply to the selected items.
Pressing the back button then cancels the action mode.
It is beyond Stackoverflow's scope to explain the whole action mode system, but you can simply search for it in the internet.
Here is a tutorial for beginners, for example.
I can give you a better answer if you post your code.
You'd probably want your adapter to contain a list of selected items. If the list has any elements, you show the checkboxes and check the ones that correspond to the list. If it is empty, you don't do that. You're correct about notifying data set changed. Long press on an item would add that item to this list. Then when your adapter re-laid out your items, it would show the checkboxes since that list would not be empty.
For the second issue, you'd want to override onBackPressed() in your activity. Then you can have some code like:
public void onBackPressed() {
if (adapter.hasItemsSelected()) {
adapter.clearSelection()
} else {
super.onBackPressed()
}
}
You'll need to write these adapter methods. hasItemsSelected should check to see if there are elements in the selected items list and clearSelection should clear the list and notify data set changed.

changing listview item elements

I'm new to Android development.
I need to make a listview with text and icon audio player. The icon has three States, "download", "downloading", "play" and "pause". The idea is that when populating the listview, set the "download" icon if the track is not downloaded and "play" if the track is downloaded". If a person clicks the "download" button the icon should be updated to "downloading" to start the download, then when complete, you should see the icon "play". If the user presses the "play" icon for this element should be updated to "pause".
I can't change the icon . I tried to change them in onItemClickListener and make the onClick in the getView method in adapter, but when scrolling the icons changed to the old. And if you select multiple items in the list, then change all the icons, and I need to change only one, for playing of the track.
I didn't show the code because there is nothing that can be corrected. - all wrong
if you have your own adapter try using a method notifyDataSetChanged();
That is happening due to recycling of views. If you click on an item and you will see multiple items clicked.
Use RecyclerView instead of listView, which will handle recycling by its own and its ViewHolder class takes care of the issue you are having.
And don't forget to notify the recyclerView adapter whenever you make some change in data source (i.e. arraylist)
Check out this example...
Hope it helps
http://hmkcode.com/android-simple-recyclerview-widget-example/

How to dynamically remove random views in Android?

So here is what my app looks like so far. Every time I click the "+" button I go into another activity where I enter description, date and time and dynamically create a horizontal LinearLayout. With the X button to the left I'm deleting said layouts with this code (I know it's not the best way but it works for me so far):
final Task toBeRemoved = x;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myLayout.removeView((ViewGroup) v.getParent());
Task.tasks.remove(toBeRemoved);
}
});
..while iterating through each element in a list where I store my values in my OnCreate method.
What I want to do now is make it so I can remove them with the assistance of the checkboxes and the "Clear" button as well.
I have added each layout dynamically, though, so I can't think of any way for me to determine which one I've checked for deletion. They have no id, they can't be stored anywhere so I can iterate through them, as far as I know. What can I do in this situation?
A couple of ways. One would be to set listeners on each new checkbox and keep a Set of views with a checked state (add to the Set when checked, remove when unchecked). Then when clear is pressed, you remove all views in that Set.
The other way is to lopp through all the child views of the parent layout above the dynamically. For each one, find the checkbox child via findViewById, and see if its check. Remove it if it is. This is computationally expensive if you have lots of complex views
I prefer method 1 myself, but either works.
I have added each layout dynamically, though, so I can't think of any
way for me to determine which one I've checked for deletion
that's not true. You can call setId( ) or setTag() while you are adding each layout,
They have no id, they can't be stored anywhere so I can iterate
through them, as far as I know. What can I do in this situation?
now they have one. You can either use findViewById or findViewWithTag
Here's a solution that I consider pretty cool.
Create a widget on your own.It's easy.
So, you would have a class that extends ...probably LinearLayout.Depends on your needs.
You create the button,the editText, the textview and the check box, than you add functionality like you want.
When the Check box is pressed, I suppose you want the X circle button to get activated and when you press it, it deletes the whole "thing".This way you won't even need the clear button.You will still need the add button though, so you can add how many of those items you need.
It's easy,practical and makes the code reusable, which is something I look for always.
If you need more help, I can help you in a bit more detail ,but I can't always be around so I am sorry if I will respond a bit slow.

Listview items show they've been clicked when they haven't

Hey so i've got a listview with buttons on it, when one is clicked if you skip down 4 more listview items the fifth shows it's been clicked in the same place the previous one was clicked. When a button is clicked it is set to invisible and the dummy button underneath is shown (which is the one that shows up in every fifth row).
Code that declares button invisible in the onclick command
boolean processClick=true;
if(processClick == true)
{
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}
processClick=false;
}
After looking at some similar questions, I think this has to do with the views being recycled, but still not sure how to remedy this.
Any helps at all is much appreciated.
Thanks!
Yes u are having problem with your code,you need to take processClick for each listView row/item and then on that bases u need to set enabled(false) or something else.You can take reference from this example which is similar work but with checkbox. You need to look at that code and apply with button. It'll solve your problem.
link of sample ex. https://drive.google.com/file/d/0B6C9Pqrvc_CWZHFDcmxKR01rc3c/edit?usp=sharing
I faced a similar problem when I was programatically assigning background color to my listview items. I dont know what caused the problem (probably the getView() was not getting called properly). I solved my problem by using if-else. In your case something like this should work:
if(processClick == true)
{//set desired result in if
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}else{//reset items to original in else
myButton.setEnabled(true);
myButton.setClickable(true);
myButton.setVisibility(View.GONE);
}

Is it possible to make a dynamic mix of ListView and TextView?

I tried to found a method, but no results. I want a ListView like below, and when I click on an element, like "Word", it'll be like this picture :
Is it possible ?
What you described is what an ExpandableListView does.
Basically it's like a listview so you'll still have to create your own adapter, but it lets you click a row to inflate a bigger item that you can then stuff your text into.
I would recommend creating your own class that extends ArrayAdapter or BaseAdapter. Then you can make use of the getView() method that gets called every time the screen gets redrawn for the user. You can then design multiple views for each selection and choose which one to display in the getView() function. So when the user selects an item, you set a flag in your custom class, and then notifyDataSetChanged() and you're good to go!

Categories

Resources