1

I am not able to do the simpliest things :-) ( I guess it should be simple )

I try to highlight a Item in a listview when clicking on a button. I click and nothing happens :-(

Does anyone see, what I am doing wrong with following code, or does anyone have a hint ?

When starting the activity the listview is shown and also the third item is highlighted (int isymbolindex=2;). But when try to change the selection in the onClick or onItemClick it doesn't wotk. When Debugging it goes through the 'public View getView' method and isymbolindex=3, but gui (listview ) is not updated.

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ExpandableListActivity;
    import android.content.Context;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ExpandableListView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class CityEditorActivity extends Activity {

    int isymbolindex=2;     
    Context croot=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_editor);
        final ListView lvsymbols= (ListView)findViewById(R.id.listViewdymbols);


        String[] values = new String[] { "Value1", "Value2", "Value3", "Value4"};

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }

        lvsymbols.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvsymbols.setSelector(android.R.color.darker_gray);


        Button bvon1=(Button)findViewById(R.id.vonadd1); 
        bvon1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                isymbolindex=3;
                lvsymbols.setSelection(3);
                lvsymbols.invalidate();
            }
        });




        lvsymbols.setAdapter(   new ArrayAdapter(this, R.layout.own_simple_list_item, list)
        {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final View renderer = super.getView(position, convertView, parent);
                if (position == isymbolindex)
                {
                    //TODO: set the proper selection color here:
                    renderer.setBackgroundResource(android.R.color.darker_gray);
                }else{
                    renderer.setBackgroundResource(android.R.color.white);
                }
                return renderer;
            }

        });

        lvsymbols.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long arg3) {
                isymbolindex=position;
                arg0.setSelection(isymbolindex);

                view.getFocusables(position);
                view.setSelected(true);
                lvsymbols.invalidate();
            }
        });


    }


}
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • When you say nothing happen, you mean no highlight ? or the code in onClick is no executed ? – An-droid Sep 06 '13 at 08:13
  • Sorry for the confusing description. I meant gui is not updating. ListView.setItemChecked(int position, boolean checked) in the next post helped me. Thanks a lot. – mcfly soft Sep 06 '13 at 08:25

2 Answers2

1

You can try requestFocusFromTouch() before calling setSelection() method

or

maybe.

ListView.setItemChecked(int position, boolean checked);
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
An-droid
  • 6,433
  • 9
  • 48
  • 93
  • 1
    Great !!! ListView.setItemChecked(int position, boolean checked) helped me. Thanks a lot. – mcfly soft Sep 06 '13 at 08:24
  • Do you @Yume117 know why this can't work for me? [link](http://stackoverflow.com/questions/18671826/highlight-listview-item-at-creation-time-via-code) – Accollativo Sep 08 '13 at 13:01
0

You can try this also

On the main layout in which you have your List View add this

android:background="?android:attr/activatedBackgroundIndicator"

Create A Selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true">
<shape>
    <solid android:color="#ffffff" />
</shape>

In your activity

listView.setSelector(R.drawable.listview_item_selection_effect);
listView.setItemChecked(3, true);


onClick of button call this setItemChecked and pass the postion of the item
Developer
  • 6,292
  • 19
  • 55
  • 115