所有切换按钮的状态

所有切换按钮的状态

如何获取列表视图android中所有切换按钮的状态

public View getView(int position, View convertView, ViewGroup parent)
    {
        pos = position;

        ApplicationInfo entry = ai.get(position);
        String str = insatllApps.get(position);
        System.out.println("Position============"+(insatllApps.get(position)));
        //Log.d("Check>>:","size>>"+insatllApps.size());
        convertView = mInflater.inflate(R.layout.list__button, null);
        t1=(TextView)convertView.findViewById(R.id.textView1);
        iv=(ImageView)convertView.findViewById(R.id.imageView1);
        tb1=(ToggleButton)convertView.findViewById(R.id.togal);
        tb1.setOnClickListener(this);
        tb1.setTag(pos);
        tb1.setTag(R.id.togal, str);

        iv.setImageDrawable(entry.loadIcon(mPackManager));


        t1.setText(str);


        return convertView;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
        case R.id.togal:

            String str = ""+v.getTag(R.id.togal);
            boolean status;

            Log.d("samit", "str>>>>"+str);

            //Integer in =  Integer.getInteger(str);
            //Log.d("samit", "in>>>>"+in);
            //int i =in.intValue();
            //Toast.makeText(context, "samit id>>"+v.getTag(), Toast.LENGTH_LONG).show();
            Toast.makeText(context, "samit id>>"+str, Toast.LENGTH_LONG).show();
            //Log.d("samit", "id>>>>"+i);

            break;
        }
    }

在这里我想得到列表视图中所有切换按钮的状态

最佳答案

使用arrayadapter而不是baseadapter可以解决滚动问题。
接下来在onstop()中保存sharedpreference中的值,onstart()从sharedpreference中检索值

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{

        }
    }

 }
}

10-06 06:56