baseAdapterの代わりにArrayAdapterを使用すると、これを使用してスクロールの問題を解決できます。次にonStop()で値を共有設定に保存し、onStart()で共有設定から値を取得します
public class PlanetArrayAdapter extends ArrayAdapter<Planet> {
private LayoutInflater inflater;
public PlanetArrayAdapter( Context context, List<Planet> planetList ) {
super( context, R.layout.checklist, R.id.textView1, planetList );
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context) ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
Planet planet = (Planet) this.getItem( position );
// The child views in each row.
ToggleButton checkBox ;
TextView textView ;
// Create a new row view
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.checklist, null);
// Find the child views.
textView = (TextView) convertView.findViewById( R.id.textView1 );
checkBox = (ToggleButton) convertView.findViewById( R.id.checkBox1 );
// Optimization: Tag the row with it's child views, so we don't have to
// call findViewById() later when we reuse the row.
convertView.setTag( new PlanetViewHolder(textView,checkBox) );
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ToggleButton cb = (ToggleButton) v ;
Planet planet = (Planet) cb.getTag();
planet.setChecked( cb.isChecked() );
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call findViewById().
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox() ;
textView = viewHolder.getTextView() ;
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag( planet );
// Display planet data
checkBox.setChecked( planet.isChecked() );
textView.setText( planet.getName() );
return convertView;
}
}
//私のアクティビティクラスは...です。
public class MainActivity extends Activity {
private ListView mainListView;
//private Planet[] planets;
ArrayList<Planet> planets=new ArrayList<Planet>();
private ArrayAdapter<Planet> listAdapter;
/** Called when the activity is first created. */
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
mainListView = (ListView) findViewById(R.id.listView1);
// When item is tapped, toggle checked properties of CheckBox and
// Planet.
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Planet planet = listAdapter.getItem(position);
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item
.getTag();
viewHolder.getCheckBox().setChecked(planet.isChecked());
}
});
// Create and populate planets.
//planets = (ArrayList<Planet>) getLastNonConfigurationInstance();
planets.add(new Planet("1", false));
planets.add(new Planet("2", false));
planets.add(new Planet("3", false));
planets.add(new Planet("4", false));
planets.add(new Planet("5", false));
planets.add(new Planet("6", false));
planets.add(new Planet("7", false));
planets.add(new Planet("8", false));
planets.add(new Planet("9", false));
planets.add(new Planet("10", false));
planets.add(new Planet("11", false));
planets.add(new Planet("12", false));
planets.add(new Planet("13", false));
planets.add(new Planet("14", false));
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planets);
mainListView.setAdapter(listAdapter);
}
public Object onRetainNonConfigurationInstance() {
return planets;
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
for(int i=0;i<planets.size();i++){
Planet planet = listAdapter.getItem(i);
planet.toggleChecked();
System.out.println("position is checked"+i+"="+planet.isChecked());
SharedPreferences sp=getSharedPreferences("MY_DATA",MODE_PRIVATE);
SharedPreferences.Editor et=sp.edit();
et.putString(""+i, ""+planet.isChecked());
et.commit();
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
SharedPreferences pf=getSharedPreferences("MY_DATA", MODE_PRIVATE);
for(int i=0;i<planets.size();i++){
Log.v("===i===",""+pf.getString(""+i,""));
if(pf.getString(""+i,"").equals("false")){
planets.set(i, new Planet(""+i, true));
}
else{
}
}
}
}