0

我想选中列表视图中的所有复选框,但我无法从列表视图中获取复选框对象。我可以选择一个复选框,但不能选择多个复选框。

你的建议是可观的。

代码:

   public class MainActivity extends Activity {
   @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bir);
    mainListView = (ListView) findViewById(R.id.mainListView);
    selectall = (Button) findViewById(R.id.button1);
    selectall.setOnClickListener(this);
    save = (Button) findViewById(R.id.button2);
    save.setOnClickListener(this);

    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener()

            {
                @Override
                public void onItemClick(AdapterView<?> parent, View item,
                        int position, long id) {

                }
            });
  }
}

两栖类:

 private static class Amphian 
        {
            private String name = "" ;
            private boolean checked = false ;
            public Amphian( String name ) 
            {
              this.name = name ;
            }
            public Amphian( String name, boolean checked )
            {
              this.name = name ;
              this.checked = checked ;
            }
            public String getName() {
              return name;
            }
            public void setName(String name) {
              this.name = name;
            }
            public boolean isChecked() {
              return checked;
            }
            public void setChecked(boolean checked) {
              this.checked = checked;
            }
            @Override
            public String toString() {
              return name ; 
            }
            public void toggleChecked() 
            {
              checked = !checked ;
            }
        }

AmphiansArrayAdapter 类:

    public class AmphiansArrayAdapter extends ArrayAdapter<Amphian> 
         {
             Integer name[] =
                {
                     R.raw.ducks_landing_in_water, 
                     R.raw.flicker_chicks_feeding, 
                     R.raw.geese_honking_loud, 
                     R.raw.geese_honking_distant, 
                     R.raw.gold_finch, 
                     R.raw.humming_bird_feeding, 
                     R.raw.indigo_bunting, 
                     R.raw.loons,
                     R.raw.little_blue_heron_fishing,
                     R.raw.pelican_chick,
                     R.raw.purple_martins,
                     R.raw.red_winged_blackbird,
                     R.raw.shorebirds_close,
                     R.raw.shorebirds_distant,
                     R.raw.shorebirds_misc,
                     R.raw.shoreseabirds,
                     R.raw.snow_geese_flock,
                     R.raw.terns,
                     R.raw.tufted_titmouse,
                     R.raw.tundra_swans,
                     R.raw.wood_stork_chicks,
                     R.raw.woodpecker_tapping

                };

            private final LayoutInflater inflater;

            public AmphiansArrayAdapter(Context context, List<Amphian> amphianList) 
            {
                super( context, R.layout.simplerow, R.id.rowTextView, amphianList );
                 inflater = LayoutInflater.from(context) ;
            }

            @Override
            public View getView( final int position,  View convertView , ViewGroup parent)
            {
                final Amphian amphian=this.getItem(position);

                mp=new MediaPlayer();
                 if ( convertView == null ) 
                 {
                        convertView = inflater.inflate(R.layout.simplerow, null);

                        // Find the child views.

                        textView = (TextView) convertView.findViewById( R.id.rowTextView );
                        checkBox = (CheckBox) convertView.findViewById( R.id.checkBox1 );

                        button   = (Button)convertView.findViewById(R.id.button1);

                        // 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 AmphianViewHolder(textView,checkBox,button) );

                        // If CheckBox is toggled, update the planet it is tagged with.
                        checkBox.setOnClickListener( new View.OnClickListener()
                        {
                          @Override
                        public void onClick(View v) 
                         {
                            cb= (CheckBox) v;
                            Log.e("cb",String.valueOf(cb));
                            Amphian amphian = (Amphian) cb.getTag();
                            Log.e("cb",String.valueOf(cb.getTag()));
                            amphian.setChecked(cb.isChecked());

                            Log.e("dd", "ddd");
                          }
                        });  

                        button.setOnClickListener(new OnClickListener()
                        {

                            @Override
                            public void onClick(View v)
                            {
                                Button bu=(Button)v;
                                Amphian amphian; 
                                //= (Amphian) bu.getTag();
                                //Log.e(String.valueOf(amphian),"ddd");
4

3 回答 3

2

尝试这个。

public class MainActivity extends Activity implements OnClickListener {
public class LVSample3Adapter extends BaseAdapter{
    private Context context;
    private List<LVSample3Item> itemList;
    public LVSample3Adapter(List<LVSample3Item> lstItems,
            MainActivity mainActivity) {
        // TODO Auto-generated constructor stub
        this.context=mainActivity;
        this.itemList=lstItems;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.e("lstItems1:", String.valueOf(position));
        LVSample3Item item = itemList.get(position);
        convertView =LayoutInflater.from(context).inflate(R.layout.list, parent, false);
        TextView t1=(TextView)convertView.findViewById(R.id.textView1);
        t1.setText(item.getTitle());
        CheckBox chb1=(CheckBox)convertView.findViewById(R.id.checkBox1);
        chb1.setChecked(item.getstate());

        return convertView;
    }



}
/** Called when the activity is first created. */
private ListView lv;
private ListAdapter adapter;
private Button btn1,btn2;
private  List<LVSample3Item> lstItems;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn1=(Button)findViewById(R.id.button1);
    btn2=(Button)findViewById(R.id.button2);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    lstItems = new ArrayList<LVSample3Item>();
    LVSample3Item item = new LVSample3Item("drinks",false);
    lstItems.add(item);
    item = new LVSample3Item("chat",false);
    lstItems.add(item);
    item = new LVSample3Item("chat1",true);
    lstItems.add(item);
    item = new LVSample3Item("chat2",false);
    lstItems.add(item);
    adapter = new LVSample3Adapter(lstItems, this);
    lv=(ListView)findViewById(R.id.listView1);
    lv.setAdapter(adapter);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.button1){
        Log.e("lstItems:", String.valueOf(lstItems.size()));
        for(int i=0;i<lstItems.size();i++){
            LVSample3Item item=lstItems.get(i);
            if(!item.getstate()){
                item.setpath(true);
            }
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();

    }else if(v.getId()==R.id.button2){
        for(int i=0;i<lstItems.size();i++){
            LVSample3Item item=lstItems.get(i);
            if(item.getstate()){
                item.setpath(false);
            }
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }

}

}

public class LVSample3Item implements Serializable {



private String title;
private boolean state;

public LVSample3Item(String title,boolean imagepath) {

    this.title = title; 
    this.state=imagepath;
}





public String getTitle() {
    return title;
}
public boolean getstate() {
    return state;
}

public void setTitle(String title) {
    this.title = title;
}
public void setpath(boolean imagepath) {
    this.state = imagepath;
}

}

于 2012-05-02T12:41:06.933 回答
1

有太多代码要阅读,所以我给你一个示例如何做到这一点:

int count = list.getCount();
for (int i = 0; i < count; i++) {
    View child = list.getItemAtPosition(i);
    //check that child..
}

或者

int count = list.getChildCount();
for (int i = 0; i < count; i++) {
    View child = list.getChildAt(i);
    //check that child..
}
于 2012-05-02T07:49:25.133 回答
1
selectall.setOnClickListener(new onClickListener(){

   @Override
   public void onClick(View v) {
       for (int i = 0; i < list.size(); i++) {
            list.getItem(i).setChecked(true);
       }
   }
});

尝试这样做

于 2012-05-02T07:51:30.187 回答