Android - Context menu no longer highlighting selected items in ListView - android

I have an app that displays a list of articles, which when clicked on changes its background color to green. The ListView is expanded by ViewPager. The color change occurs so that the user knows which articles has been read.
It is also remembered, so that the next time the user loads up the same list, items that have been clicked on remains green. The code below is what I used to make this work. The problem that I am now having is that, when pressing down long enough for the context menu to appear, my articles on ListView no longer changes color upon selection.
Before I instigated the green background change for each item that is clicked on, selecting items in the ListView (context mode) used to get grayed out. The context menu and functionality still works, but it no longer shows which items have been selected. Please can someone advise?
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// when item has been clicked on, variable is set to true.
a.setRead(true);
saveToReadList(toReadList);
.....
Custom Adapter:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
.....
/*if the article has been clicked on, then read attribute value is true and
background is green*/
if(en.isRead()){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}else{
convertView.setBackgroundColor((Color.parseColor("#ffffff")));
}
/* convertView.setBackgroundColor(Color.parseColor("#66BB6A"));*/
return convertView;

In the onClickListener() of listView item set
CheckedTextView item = (CheckedTextView)view;
if(item.isChecked()){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}

Related

Context Menu background color upon selection in ListView no longer grays out

I have a ListView of items which when you click on will change its background color (and will keep the background color even after restarting the app). This is to indicate that the item has been read (the expanded view opens in VieWPager). My problem is, since I have been able to change the background color of ListView items (for those that have been clicked), when i long press to bring up the Context Menu, when selecting on an item, the background color of that item no longer greys out like it usually does. As a result, i cannot "see" what i am selecting in context mode. I really have no idea how to fix this problem. Please can someone advise?
The code that I used to change the color of background items in ListView:
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// Clicked on items are flagged.
a.setRead(true);
saveToReadList(toReadList);
..........
Custom adapter:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
.....
//if the article has been clicked on, then read value is true. Background is set to green.
if(en.isRead()){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}else{
convertView.setBackgroundColor((Color.parseColor("#ffffff")));
}
return convertView;

Android ListActivity using SimpleAdapter - Highlight selected item

I´m using SimpleAdapter in my ListActivity and I want when user select an item highlight this item. I tried extends SimpleAdapter and override getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
return view;
}
but this solution don´t work properly. It set background color to more then one list row.
Can sameone help me?
That's because the convertView is being reused and you do not update the selected state for both cases (selected / not selected). You need to call setSelected(false) when it is not the item you want selected and reset the background color. Also, the call to setPressed is not needed.
You also should checkout the ColorStateList which will allow you to define the colors for various states. Then you could just use the built in support for single item selection.
So instead of (e.g.) opening a new Activity you want to highlight the selected list item by changing its colors if the user clicks on it?
In your ListActivity you need to override onListItemClick()
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
TextView tv = (TextView)view;
tv.setTextColor(Color.RED);
tv.setBackgroundColor(Color.BLUE);
}
This changes the color of the selected entry to red (text) and blue (background).
Thats the first step. But the first clicked entry stays this way, even if you click another one. So you need to change that (somehow).

Android, refresh listview on item clicked

I know their are a lot of question relative to refreshing listView, but none of them are anwering my problem (as far as I know).
I'm trying to change my item view when clicking on it in my list view.
To change the view I've got a boolean isClicked on my POJO :
networksListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> _adapter, View view, int pos, long id) {
myApp.networksGlobal.networks.get(pos);
myApp.networksGlobal.networks.get(pos).isClicked = !myApp.networksGlobal.networks.get(pos).isClicked;
adapter.notifyDataSetChanged();
}
});
When I click on the item isClicked switch from true to false, and on my getView listAdapted I'm switching the view type :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Network network = (Network) getItem(position);
NetworkItemView networkItemView;
if (convertView == null) {
networkItemView = NetworkItemView_.build(context);
} else {
networkItemView = (NetworkItemView) convertView;
}
networkItemView.bind(network);
if (network.isClicked) {
networkItemView.networkItemButtonLayout.setVisibility(View.VISIBLE);
} else {
networkItemView.networkItemButtonLayout.setVisibility(View.GONE);
}
return networkItemView;
}
On the first click everything is working great, the Toast is shown an the Layout networkItemButtonLayout is set to visible.
But if I click a second time on the same item : nothing happend. No Toast is shown and no changes on the Layout visibility.
If I comment adapter.notifyDataSetChanged() the Toast is shown for every click. So I guess the problem is why the call to adapter.notifyDataSetChanged(). So tried several work around (adapter.notifyDataSetChanged() and networksSearchListView.invalidateViews()) but nothing do the trick, and I can't find a way to click several time on my item.
Does I'm using 'notifyDataSetChanged' the proper way ?

Android ListView itemClick Issue

Can someone explain this issue to me ?
I have a listview that holds more rows than the screen can show, so scrolling.
If I click on one item, I replace an icon that is part of each row. That all works.
The issue I have is that when I click on lets say the first item, I change the icon for that first row. When I now scroll down I see that the first row outside the visible viewport also changed the icon.
Why is that happening and how can I avoid this issue ?
Thanks in advance,
Mozzak
Just to make sure, you are using a class that implements ListAdapter or extends some other sort of adapter right?
When using an adapter, you will have to keep in mind that the views in the ListView are recycled to save memory. Because of this, you will need to store the state in a separate variable.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater inflator = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.listitem, null);
}
// Retreive my image that may or may not change
ImageView myIcon = (ImageView) convertView.findViewById(R.id.iconView);
// Checking my stored boolean for this position to see if I need to use icon2 or icon
if (myItem[position].needsIconChanged)
{
// I have set my boolean, so use icon2
myIcon.setImageResource(R.drawable.icon2);
}
else
{
// I have not set my boolean, or set it to false so set it to icon
myIcon.setImageResource(R.drawable.icon);
}
return convertView;
}
You will also have to remember to set that boolean in your onItemCLick
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long arg3) {
// Retreive your item and set a boolean or icon state (depending on what you do)
myAdapter.getItemAtPosition(position).needsIconChanged = true;
}

Android:To set an item as selected when the ListView opens?

An activity has a Button and a ListView.
Initially, only the Button is visible. When the button is pressed, the ListView is displayed.
When displayed, is it possible for me to show one particular item as selected/focussed?
A use case could be that suppose it is a list of language settings and when the list opens, the currently selected language must be shown as highlighted.
If I know the index of the item, how to set it as focused on display?
I post my solution, because google still doesn't know the answer.
getListView().setItemChecked(selectedGroupIndex, true);
In short, ListView::setSelection(int position) is what you need. However, depending on whether the device is in touch mode or not, it may or may not have visual effect (background highlighting). For more details, refer to Android ListView Selection Problem
If you use an Adapter for your ListView add this code to your adapter:
public class MyAdapter extends ArrayAdapter<MyClass> {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflator.inflate(R.layout.my_adapter, null);
} else {
rowView = (View) convertView;
}
//...
// set selected item
LinearLayout ActiveItem = (LinearLayout) rowView;
if (position == selectedItem){
ActiveItem.setBackgroundResource(R.drawable.background_dark_blue);
// for focus on it
int top = (ActiveItem == null) ? 0 : ActiveItem.getTop();
((ListView) parent).setSelectionFromTop(position, top);
}
else{
ActiveItem.setBackgroundResource(R.drawable.border02);
}
}
private int selectedItem;
public void setSelectedItem(int position) {
selectedItem = position;
}
}
In your Activity:
myAdapter.setSelectedItem(1);
I am using an Adapter and didn't want to set custom background colors, but use the android:state_selected in drawable xml. SetSelection didn't work for me, but maybe that's also since I needed SetNotifyDataChanged which shows that the Selected State is not persistent.
I also found that the Selected state for an item in a ListView is not persistent, since SetNotifyDataChanged results in updating the ListView layout which clears them all. Setting the item to Selected in the Adapter's GetView is too soon too.
Eventually I set the Selected state for the view of the selected item after the layout of the listview has been changed, which is when LayoutChange event is being triggered (in Java it's probably attaching a to OnLayoutChangeListener of the ListView).
To make it really easy I store the view of the selected item as Adapter's SelectedItemView.
In the ListView's LayoutChange eventhandler I just set the adapter's SelectedItemView.Selected to true.
Here's the code from my Activity where I set the Adapter for the ListView and also subscribe to LayoutChange (or in Java attach an OnLayoutChangeListener)
ringTonesListView.Adapter = ringTonesListAdapter;
ringTonesListView.LayoutChange += (s, layoutChangeArgs) => {
//At this stage the layout has been updated and the Selected can be set to true for the view of the selected item. This will result in android:state_selected logic to be applied as desired and styling can be completely done per layout in Resources.
ringTonesListAdapter.SelectedItemView.Selected = true;
};
Here's my code for the Adapter:
public class RingTonesListAdapter : BaseAdapter<RingToneItem>
{
List<RingTone> Items { get; set; }
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
// re-use an existing view, if one is available
// otherwise create a new one
if (view == null)
{
view = Context.LayoutInflater.Inflate(Resource.Layout.AlertSoundItem, parent, false);
view.Click += SelectRingTone;
}
RingTone ringTone = this[position];
if (ringTone.Selected)
{
//==> Important
//Store this view since it's the view for the Selected Item
SelectedItemView = view;
//Setting view.Selected to true here doesn't help either, since Selected will be cleared after.
}
return view;
}
private void SelectRingTone(object sender, EventArgs args)
{
View view = (View)sender;
string title = view.FindViewById<TextView>(Resource.Id.ringToneTitle).Text;
RingToneItem ringToneItem = Items.First(rt => rt.Title == title);
if (!ringToneItem.Selected)
{
//The RingTone was not selected and is selected now
//Deselect Old and Select new
foreach (RingToneItem oldItem in Items.Where(rt => rt.Selected))
{
oldItem.Selected = false;
}
// Select New RingTone
ringToneItem.Selected = true;
//Update the ListView.
//This will result in removal of Selected state for all Items when the ListView updates it's layout
NotifyDataSetChanged();
}
//Now play the test sound
NotifierService.TestSound(Context, ringToneItem);
}
public View SelectedItemView { get; set; }
}

Categories

Resources