0

In My app i have implement the ListView. Now i want it to set as like that: If i select on perticular index it should be remain as selected. ans appear as selected on ListView. If i select another index then now that new index should be remain as selected.

Edited

Means I want to set as the selected index should remain as highlighted as selected till I select another. but not like multiple selected.

So how to do it ?

Please help me for that. I have implemented the ListView as like below code:

phonemesListView = (ListView) findViewById(R.id.phonemsListView);
private String[] Phonemes_List = new String[]{"P","B","T","D","K","G","N","M","ING","TH v","TH vl","F","V","S","Z","SH","CH","J","L","R rf","R b"};

phonemesListView.setAdapter(new ArrayAdapter<String>(this,R.layout.phonemes_list_row, R.id.phonemes,Phonemes_List));
    @Override
    public void onItemClick(AdapterView<?> parent, View view,final int Position,long id) {
        phonemsText.setText(Phonemes_List[Position]);
        Toast.makeText(getApplicationContext(), "Phonems: "+Phonemes_List[Position], Toast.LENGTH_SHORT).show();
//      view.setBackgroundColor(Color.RED);
//      phonemesListView.setBackgroundColor(Color.BLUE);
        jumposition = Position;             
        int temp = 0;              
        if(jumpCount == -1){                 
            view.setBackgroundColor(Color.BLUE);                  
            jumpCount = jumposition;                 
            JumpView = view;                  
            temp = 1;             
        }              
        if(temp == 0) {                 
            if(jumpCount == jumposition) {                     
                view.setBackgroundColor(Color.BLUE);                     
                JumpView = view;                 
            }                 
            else{                     
                JumpView.setBackgroundColor(Color.TRANSPARENT);                     
                view.setBackgroundColor(Color.BLUE);                      
                jumpCount = jumposition;                     
                JumpView = view;                 
            }             
        }
    }

Thanks.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • this post could give you some insight http://stackoverflow.com/questions/3825645/android-listview-with-multiple-select-and-custom-adapter and try listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); :) – Sergey Benner Jan 17 '12 at 12:43

4 Answers4

2

You can just save the selected position from the data holder that you are using to populate your ListView. Then, you can use setSelection(position) attribute of ListView to set the selected position remain selected whenever you want.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Thanks lalit. but i want is that the selected list index should remain as highlited. – Shreyash Mahajan Jan 17 '12 at 13:08
  • you probably need to implement the OnScrollListener and set your item there but there's a note to that 'An android listview recycles "items" in the list when they are not visible on the screen. So anything that is not visible will be null.' and the info about that http://stackoverflow.com/questions/4156217/android-listview-onscroll-question – Sergey Benner Jan 17 '12 at 13:14
1
    First Create the List view by using the Base adapter : As Follows Create two layout files and One java file : 

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">
        <ListView
            android:id="@+id/listviewText"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#FFFFFF"></ListView>
    </LinearLayout>

    textview.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="15dip"
                android:focusable="false"          
></TextView>

    </LinearLayout>

    Activity code is 

    package com.pac.marico;


import java.util.ArrayList;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewColor extends Activity {
    /** Called when the activity is first created. */
    ArrayList<String> arrayList;
    Listviewlistneer listviewlistneer;
    ListView listView;

    int jumposition;
    int jumpCount = -1;
    View JumpView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView)findViewById(R.id.listviewText);
        arrayList = new ArrayList<String>();

        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");
        arrayList.add("ABC");
        arrayList.add("XYZ");
        arrayList.add("PQR");

        ListviewAdapter listviewAdapter = new ListviewAdapter();
        listView.setAdapter(listviewAdapter);

        listviewlistneer = new Listviewlistneer();

    }

    class ListviewAdapter extends BaseAdapter
    {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            View rowView = view;
            Viewholder viewholder = null;


            if(rowView == null)
            {
                LayoutInflater layoutInflater = LayoutInflater.from(ListViewColor.this);
                rowView = layoutInflater.inflate(R.layout.textview, null);

                viewholder = new Viewholder();
                viewholder.textView = (TextView)rowView.findViewById(R.id.textview);

                rowView.setTag(viewholder);
            }
            else
            {
                viewholder = (Viewholder)rowView.getTag();
            }

            viewholder.textView.setTag(position);
            viewholder.textView.setText(arrayList.get(position));


            if(jumpCount == position)
            {
                JumpView = rowView;
                            rowView.setBackgroundColor(Color.RED);
                    }
            else
            {
                rowView.setBackgroundColor(Color.TRANSPARENT);
                    }

            listView.setOnItemClickListener(listviewlistneer);
            return rowView;
        }
    }

    class Listviewlistneer implements OnItemClickListener
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

            jumposition = position;
            int temp = 0;

            if(jumpCount == -1)
            {
                view.setBackgroundColor(Color.RED);

                jumpCount = jumposition;
                JumpView = view;

                temp = 1;
            }

            if(temp == 0)
            {
                if(jumpCount == jumposition)
                {
                    view.setBackgroundColor(Color.RED);
                    JumpView = view;
                }
                else
                {
                    JumpView.setBackgroundColor(Color.TRANSPARENT);
                    view.setBackgroundColor(Color.RED);

                    jumpCount = jumposition;
                    JumpView = view;
                }
            }
        }

    }

    class Viewholder
    {
        TextView textView;
    }
}

Just Try it now.

mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51
  • Yes this is what i want but still there are some problem in it. While i am viewing the listview then there is bed appearence. and while i select on anothere then i can able to select another index. now there are two index as selected. I dont want like this. – Shreyash Mahajan Jan 19 '12 at 10:47
  • If you are setting the colour in to the onItem clic then why it is not possible for my code ? Please refer it and let me know. – Shreyash Mahajan Jan 19 '12 at 10:49
  • @iDroid: Please comment on your answer. And See the Updated question. I have done like this but still not able to get the proper result. With your answer i got the result as i have right now with my code. so please help me for that. – Shreyash Mahajan Jan 19 '12 at 12:10
  • @iDroidExplorer Can you just copy paste my program and run it. and check whether it is working. And Satisfy your requirement. then edit the program. the arraylist just you have to change. – mayur rahatekar Jan 19 '12 at 12:23
  • yes you are right but with doing that i am not getting proper result as i want. So whats the problem with that ? – Shreyash Mahajan Jan 20 '12 at 05:50
  • Your code that i have set in my code works fine but while i am viewing the listview, the highlited index is changed. i mean i got heighlited color on other index. – Shreyash Mahajan Jan 21 '12 at 06:41
0

after days of search and pulling my hair i just found out that activatedBackgroundIndicator is also available in ActionBarSherlock styling system. Most of the devs which need to develop in ICS and support backward compatibility, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases.So instead of using android:background="?activatedBackgroundIndicator" which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator"

here is the example xmle code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
   //note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:textSize="15sp" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingRight="5dip"
    android:textSize="20dip" />
  </LinearLayout>
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75
0

You can have a list of selected phonemes that is updated on onItemClick in the OnItemClickListener of the ListView and them call to adapter.notifyDataSetChanged() so the list is repainted again.

In getView method of the adapter you can change the item background if the phoneme is in the list of selected phonemes.

Regards.

sgallego
  • 318
  • 3
  • 15
  • @iDroidExplorer Are you checking on the Emulator or inside the mobile. I have checked above code in both side no error found... can you tell me what is the exact problem. – mayur rahatekar Jan 19 '12 at 10:53