Mantener seleccionado un item del ListView

public class MantMatriculas extends Activity {
    private ListView lstMatriculas;
    private View lastSelectedView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mant_matriculas);

        lstMatriculas = (ListView)findViewById(R.id.lstMatriculas);

        ArrayList<String> values =
                new ArrayList<String>();

        values.add("matricula1");
        values.add("matricula2");
        values.add("matricula3");
        values.add("matricula4");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

        lstMatriculas.setAdapter(adapter);


        lstMatriculas.setItemsCanFocus(false);
        lstMatriculas.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


        lstMatriculas.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                                    long id) {
               
                if (lastSelectedView != null)
                {
                    lastSelectedView.setBackgroundColor(Color.TRANSPARENT);
                }
                lastSelectedView = view;

                view.setBackgroundColor(Color.CYAN);
            }
        });
    }
}

No hay comentarios:

Publicar un comentario