last day (15 days later) » 

7:22 PM
-1
A: List item won't stay selected despite setting necessary properties

Gennadii SaprykinAndroid ListView reuses its views while you scroll it. You need to store information about a selected item in an item object and check if it is selected inside getView. Call setItemSelected if it is selected and setItemNormal otherwise. This way you will update the selected state of each item w...

 
Take a look at Master/Detail Flow project in Android Studio, it does exactly what you need. AS -> File -> New Project -> Master/Detail Flow
you need to call setActivatedPosition when you click on an item. if you don't call then your activated position is never updated. Highlight the item depending on mActivatedPosition value
I don't think this is a good practice to have so many fragments. Are they really different? Can you create one fragment instead of 20 and replace its content when you select an item in the list? This is how it should be implemented in most cases. Use different fragments only if they are completely different.
Anyway, if you still want to use different fragment for each item, you need to store related fragment tag or id in the item object and then use it when you click on the item.
take a look at this article, it has many examples: vogella.com/tutorials/AndroidListView/article.html
example #14 shows how to store multiple selection. In your case it's even easier, you just need to store one selection. Take a look at this example and simplify it for your needs
there is absolutely no difference. Instead of onCheckedChanged you have onItemClick. you just update the state when this event is fired like this: element.setSelected(!element.isSelected())
use list item view itself
what kind of adapter are you using?
no, this is not seen in your code. All I know is you use MainListAdapter. I have no idea whether it is ArrayAdapter or BaseAdapter. It doesn't even matter because your question is absolutely unclear.. You have an example of the list view with selection mode which is compatible with any type of adapter. Every adapter has a viewHolder and getView callback - this is all you need. Just read something about how ListView and adapter interact in Android and you will be able to solve the issue on your own.
 
I think you were talking about the class containing my getters and setters (which is now above). My question is not unclear + if it was then you wouldn't have been able to give an answer. You said yourself "Call setItemSelected if it is selected and setItemNormal otherwise.", which is what I HAVE done & it does highlight the selected item but obviously the problem relates to my list view reusing its view whilst I scroll. All that needs to be done here is to prevent this from happening. Please have a look at my code again and suggest some helpful code that can be used to solve this problem.
 
Please add your adapter's getView implementation and your view holder. Thanks.
 
I don't think mine has those things. My app runs as normal with the code you see above hence I don't know what code is necessary to add in order to fix this.
 
wait, you don't own MainListAdapter? Is it a part of some library?
 
7:22 PM
Just double checked. MainListAdapter can be seen in FragmentMain.java
 
I meant the source code of MainListAdapter..
try to modify your adapter, I've updated my answer
 
That adapter code is on the right but when I select an item, "Item 0" always appears. Please check my latest screenshot as well as MainListAdapter.java, MainActivity.java and FragmentMain.java to see if you can figure out where I've gone wrong and/or what I haven't done.
 
you are comparing your main object with a String, which is always false: main.equals(view.getResources().getString(R.string.item0)). You have to fix all those 20 conditions
 
I still don't understand what to change them to when when I have clearly used else if statements.
 
Updated my post. Please try
 
7:22 PM
OK cool, that's been fixed. All that's left is the item highlight issue. Please have a look at Main.java (in the "data" package) and FragmentMain.java then you'll notice that the item highlighted related code has been used in both these classes. Do you know what I need to do to the one in ****? I was thinking of getting rid of it but I wanna double check with you first.
 
what do you mean by highlight issue. is it still not working?
I thought it was fixed
 
So did I
...but unfortunately it seems not
OK cool, that's been fixed. All that's left is the item highlight issue. Please have a look at Main.java (in the "data" package) and FragmentMain.java then you'll notice that the item highlighted related code has been used in both these classes. Do you know what I need to do to the one in FragmentMain.java? I was thinking of getting rid of it but I wanna double check with you first.
the above was what I was meant to say in the other message
Btw it is still not working :-(
 
one sec
 
sure
 
7:41 PM
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Main selectedMain;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// get the adapter, then get the name from the adapter at that position
MainListAdapter adapter = (MainListAdapter) parent.getAdapter();
Main main = adapter.getItem(position);

if (mTwoPane) {
View rowView = view;
setItemSelected(main, rowView);


Fragment newFragment;
switch (position) {
case 0:
newFragment = new Fragment0();
try this
if that works I'll let you know how to improve that
for now lets just try this
I have removed setItemNormal method call and updated setItemSelected method
 
ok
just a min (or 2) :-)
Where did you get selectedMain from?
 
I've added that to your OnItemClickListener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Main selectedMain;
 
7:56 PM
OK almost there :-) the view are not reused when I scroll (thanks for that), however after tapping Item 18 then Item 17 then Item 16 they all get highlighted rather than highlighting one at a time
 
hmm
oh
I'm an idiot
one sec
 
No, you're not lol
It's just human error
 
public void setItemSelected(Main main, View view) {
View rowView = view;
TextView tv0 = (TextView) rowView.findViewById(R.id.item);
TextView tv1 = (TextView) rowView.findViewById(R.id.item_description);
if (selectedMain != null) {
selectedMain.setSelected(false);
view.setBackgroundColor(Color.TRANSPARENT);
tv0.setTextColor(Color.WHITE);
tv1.setTextColor(Color.parseColor("#B5B5B5"));
} else {
view.setBackgroundColor(Color.parseColor("#1C3F96"));
tv0.setTextColor(Color.parseColor("#FFFFFF"));
tv1.setTextColor(Color.parseColor("#FFFFFF"));
try this
I'll be back in 30 mins
 
Not quite
I'll be back also, but will stay logged in whilst I'm away
BTW I selected Item 3 first then Item 5
 
8:41 PM
I'm bacjk
 
aghh.. lets try this
public void setItemSelected(Main main, View view) {
View rowView = view;
if (selectedMain != null && selectedView != null) {
selectedMain.setSelected(false);
selectedView.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = (TextView) selectedView.findViewById(R.id.item);
TextView tv1 = (TextView) selectedView.findViewById(R.id.item_description);
tv0.setTextColor(Color.WHITE);
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
selectedView = view;
selectedMain = main;
selectedMain.setSelected(true);
even if it works, the code shouldn't look like that, it's ugly :) if you used selectors instead, you wouldn't have to write that much code
so it would be better if you refactor this a little bit
 
After using that I get this:
 
yeah, add selectedView too
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Main selectedMain;
private View selectedView;
 
8:56 PM
Excellent! Problem solved. :-)
 
whew :)
glad to hear that
 
I'll up-vote on all your comments
 
but I highly recommend to use selectors instead of setting colors in runtime
they will simplify the code a lot
 
Hmmmmm
Never done that before hence that's why I done it this way
 
how it works now:
I keep a reference to the selected view, then I update all views manually, all colors for each view
how it should be:
1. you create a selector which is an xml file that defines colors for different view states, e.g.
<item android:state_selected="true" color="#1C3F96"/> <item android:state_selected="false" color="#FFFFFF"/>
2. you set the selector as a background to your views
then, in code, all you have to do is to call setSelected on a root view
just selectedView.setSelected(true);
Android will handle all views in the hierarchy for you, it will update the state for all views automatically
instead of setting all colors manually you just mark a view as selected and you are done
the code will be much cleaner and also there will be no UI interaction at runtime. It's always better to have all UI related stuff in XML
hope that helps
 
9:04 PM
It does, thanks. I've had this all long and before posting the question but never used it. This is a staring point:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="@drawable/item_selected" android:state_pressed="true"/>

<!-- default -->
<item android:drawable="@drawable/item_normal"/>
</selector>
 
yes, so create as many selectors as you need. as I understand you need 1 for background and 1 for text views
set them as a background and then those views will automatically change their state when you select them
 
So do I need to put my text views and background colour in @drawable/item_selected?
Something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:drawable="@drawable/selected_item"/>
<item android:drawable="@android:color/transparent" />
</selector>
 
9:25 PM
All done :-)
 
cool
see ya
 
See ya
Thanks for everything
Onto my next issue
 

last day (15 days later) »