0

Following this post i do that:

In my ListView I set the background color of the selected item like this:

<ListView
   android:id="@+id/list_date"
   android:choiceMode="singleChoice"
   android:listSelector="#666666"
/>

When the user click an item on my list it stay highlighted, nice.

Now I want pre-select (highlight) an item of the list on start.I tried with that:

list_date.performItemClick(list_date, 0, list_date.getItemIdAtPosition(0));

but don't work. How can i do that?


Final working code:

TextView txtv,txtv2;
arrayAdapter = new ArrayAdapter<String>(HistoryActivity.this, R.layout.row, R.id.textViewList, date) {
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.row, parent, false);
                }
                txtv = (TextView) convertView.findViewById(R.id.textViewList);
                txtv.setText(date.get(position));
                if(position == 0){
                    txtv.setBackgroundColor(Color.parseColor("#93581c"));
                    txtv2 = (TextView) convertView.findViewById(R.id.textViewList);
                }
                return convertView;
            }
        };

list_date.setAdapter(arrayAdapter);

    list_date.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            if (position!=0)
                    txtv2.setBackgroundColor(Color.TRANSPARENT);
            ........
            ........
        }
}
Community
  • 1
  • 1
GMX
  • 950
  • 1
  • 14
  • 29

2 Answers2

1

You can apply the background color manually in your adapter :

arrayAdapter = new ArrayAdapter<String>(HistoryActivity.this, R.layout.row, R.id.textViewList, array) {
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.R.layout.row, parent, false);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.textViewList);
            tv.setText(array[position]);
            if(position == 0)
                tv.setBackgroundColor(Color.parseColor("#666666"));
            return convertView;
        }
    };
rhari
  • 1,367
  • 1
  • 11
  • 19
  • I updated my question for you, seem that your solution work but just for an half. – GMX Nov 19 '16 at 16:02
  • No this code should not be in your onItemClickListener, it should be in your adapter's getView method. – rhari Nov 19 '16 at 16:06
  • Oh, How can i create this getView adapter? The only adapter i have is this: "arrayAdapter = new ArrayAdapter(HistoryActivity.this, R.layout.row, R.id.textViewList, array);" – GMX Nov 19 '16 at 16:15
  • Updated my answer, check http://www.survivingwithandroid.com/2012/10/android-listview-custom-adapter-and.html for more info – rhari Nov 19 '16 at 16:47
  • Nice with this code now the first item is highlighted on start, the problem is that still highlighted even when i click another item. – GMX Nov 19 '16 at 17:29
  • I uploaded my question with the solution, btw tell me if you have some more elegant code. – GMX Nov 19 '16 at 17:54
0

For that you can use saveInstanceState to save the state. Here's official documentation Follow Documentaion

Apple Appala
  • 329
  • 4
  • 16