我一直在搜索如何在自定义列表视图中保存togglebutton的状态。我有一个带有自定义适配器的listview,它有一个文本,togglebutton和一个imageview,想法是当切换按钮时,imageview改变以显示一个灯泡,我需要保存灯泡打开和关闭的状态。
这是我的代码:
的活动:
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;
import Adapters.MenuLucesAdapter;
import Adapters.MenuLucesDimAdapter;
public class MenuLuces extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.menu_luces);
ListView list1, list2;
String[] iluminacionGrupos = getResources().getStringArray(R.array.iluminacion_Grupos);
String[] iluminacionNombres = {
"Nombre 1",
"Nombre 2",
"Nombre 3",
"Nombre 4",
"Nombre 5",
"Nombre 6",
};
String[] iluminacionDimNombres = {
"Nombre Luz Dimeada",
"Nombre Dim",
"Nombre Dim",
"Nombre Dim",
"Nombre Dim",
"Nombre Dim",
};
Boolean bulbState = false;
Integer bulbDim = 0;
// Array of booleans to store toggle button status
boolean[] estadolamparasonoff = { false, false, false, false, false ,false };
if (savedInstanceState != null)
{
estadolamparasonoff = savedInstanceState.getBooleanArray("estadolamparasonoff");
}
//**************** ListView1*****************//
list1 = (ListView) findViewById(R.id.ListView1);
MenuLucesAdapter adapter = new MenuLucesAdapter(this, iluminacionGrupos, iluminacionNombres, bulbState);
list1.setAdapter(adapter);
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("MenuLuces", "Item seleccionado " + position);
if (position == 0) {
//Intent menuLucesIntent = new Intent(getApplicationContext(), MenuLuces.class);
//startActivity(menuLucesIntent);
}
}
});
//**************** ListView2*****************//
list2 = (ListView) findViewById(R.id.ListView2);
MenuLucesDimAdapter adapterdim = new MenuLucesDimAdapter(this, iluminacionGrupos, iluminacionDimNombres, bulbDim);
list2.setAdapter(adapterdim);
list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("MenuLucesDim", "Item seleccionado " + position);
// Dependiendo de la posicion creamos un Intent para abrir la Activity correspondiente
if (position == 0) {
//Intent menuLucesIntent = new Intent(getApplicationContext(), MenuLuces.class);
//startActivity(menuLucesIntent);
}
}
});
}
}
适配器:
package Adapters;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MenuLucesAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] iluminacionGrupos;
private final String[] iluminacionNombres;
boolean [] switchState = new boolean[100]; //
private final Boolean bulbState;
public MenuLucesAdapter(Activity context,
String[] iluminacionGrupos, String[] iluminacionNombres, Boolean bulbState) {
super(context, R.layout.menu_luces_listview, iluminacionGrupos);
this.context = context;
this.iluminacionGrupos = iluminacionGrupos;
this.iluminacionNombres = iluminacionNombres;
this.bulbState = bulbState;
}
public static class ViewHolder {
TextView txtGrupo;
TextView txtNombre;
ImageView estadoBulbo;
ToggleButton botonlamp;
}
ViewHolder holder = new ViewHolder();
//Se asocia el Layout con el Listview customizado para cada item de la lista segun la variable position
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//---print the index of the row to examine---
Log.d("Luces ON/OFF",String.valueOf(position));
View v = convertView;
if (v == null) {
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.menu_luces_listview, null, true);
//---get a reference to all the views on the xml layout---
holder.txtGrupo = (TextView) v.findViewById(R.id.textView_LucesGrupo);
holder.txtNombre = (TextView) v.findViewById(R.id.textView_LucesNombre);
holder.estadoBulbo = (ImageView) v.findViewById(R.id.imageView_Bulb);
holder.botonlamp = (ToggleButton) v.findViewById(R.id.toggleButton_LucesOnOff);
v.setTag(holder);
}
else {
// Get the ViewHolder back
holder = (ViewHolder) v.getTag();
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
holder.botonlamp.setChecked(sharedPreferences.getBoolean("toggleButton"+position, false)); //default is false
//---customize the content of each row based on position---
holder.txtGrupo.setText(iluminacionGrupos[position]);
holder.txtNombre.setText(iluminacionNombres[position]);
/*
holder.botonlamp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//int index = (Integer) v.getTag();
//Log.i("tag of switch==",""+index);
*/
holder.botonlamp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton"+position, holder.botonlamp.isChecked());
editor.commit();
if(holder.botonlamp.isChecked())
{
holder.estadoBulbo.setImageResource(R.drawable.bulb_led_on);
}
else
{
holder.estadoBulbo.setImageResource(R.drawable.bulb_led_off);
}
}
});
/*
if(switchState[position])
holder.estadoBulbo.setImageResource(R.drawable.bulb_led_on);
else
holder.estadoBulbo.setImageResource(R.drawable.bulb_led_off);
*/
holder.botonlamp.setTag(new Integer(position));
return v;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
}
我有两个问题: 1.-单击切换按钮时,相应的图像不会相应更改,只有列表视图的最后一行会更改图像。 2.-当我返回上一个活动时,切换按钮的状态将恢复为默认状态。