2

如何获取列表视图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;
        }
    }

在这里,我想获取列表视图中存在的所有切换按钮的状态

4

2 回答 2

3

通过使用 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{

        }
    }

 }
}  
于 2013-03-06T10:22:01.560 回答
2

如果您想要为支持数组中的每个项目切换,这将不起作用。listView 只保留 N 行,其中 N 是适合屏幕的任何内容。上面或下面的行实际上并不存在,因此您无法查询切换的值。相反,您需要做的是保存一个数组,该数组包含切换应该是的值,并在该位置可见时设置这些值。

例如,我看到您有一个 ApplicationInfo 数组和一个您为该职位获得的字符串数组。相反,您应该有 1 个 ListItems 类数组,其中包含每行的 ApplicationInfo、名称和切换值。当调用 getView 时,您将这些值填充到行视图中。当有人切换开关时,您应该有一个 onClickListener,它将新值保存回 ListItems 数组。

于 2013-03-04T04:21:48.530 回答