复选框检查按钮单击

时间:2016-12-23 05:32:27

标签: android listview checkbox

我有一个复选框的列表视图,其中第一个复选框被选中,如果用户点击下一个按钮,则页脚我有下一个按钮,然后选中下一个复选框,检查第二行。如果再次按下,则检查第三个。请帮助我坚持这么久。谢谢。

  public class PlanetArrayAdapter extends ArrayAdapter<Planet> {

    private LayoutInflater inflater;
    ArrayList<Boolean> positionArray;
    Context ctx;

    public PlanetArrayAdapter( Context context, List<Planet> planetList ) {
        super( context, R.layout.list_rowss, R.id.rowTextView, planetList );
        // Cache the LayoutInflate to avoid asking for a new one each time.
        inflater = LayoutInflater.from(context) ;
        positionArray = new ArrayList<Boolean>(planetList.size());
        for(int i =0;i<planetList.size();i++){
            positionArray.add(false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

       position_new =position;
        Planet planet = (Planet) this.getItem( position_new );      
        final CheckBox checkBox ;
        final TextView textView,textview_duration_MAX, textview_duration_Current;
        final CheckBox pauseplay; ;

        final ImageView download,fb,twitter,add;
        final SeekBar pb;

        if ( convertView == null ) {
            convertView = inflater.inflate(R.layout.list_rowss, null);


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

            checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
            pb = (SeekBar) convertView.findViewById(R.id.songProgressBar);
            pauseplay = (CheckBox)convertView.findViewById(R.id.button);
            textview_duration_MAX = (TextView)convertView.findViewById(R.id.textView3);
            textview_duration_Current =(TextView)convertView.findViewById(R.id.textView2);
            download = (ImageView) convertView.findViewById(R.id.imageView);
            fb = (ImageView) convertView.findViewById(R.id.imageView4);
            twitter = (ImageView) convertView.findViewById(R.id.imageView5);
            add = (ImageView) convertView.findViewById(R.id.imageView6);

            checkBox.setTag(position);

            // 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,textview_duration_MAX,textview_duration_Current,checkBox,pb,pauseplay,add) );



            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    Planet palnts = (Planet) buttonView.getTag();


                    Log.d("poiuy",palnts+"");//For getting checkbox postion


                    CheckBox cb = (CheckBox) buttonView;

                    Planet planet = (Planet) cb.getTag();
                    planet.setChecked(cb.isChecked());

                    boolean isStart;

                    if (checkBox.isChecked()) {


                        if (selected != null) {
                            selected.setChecked(false);
                        }

                        checkBox.setChecked(true);
                        selected = checkBox;

                        pauseplay.setVisibility(View.VISIBLE);
                        song_namef.setText(planet.getName().substring(18));
                        playButton.setImageResource(R.drawable.pausef);

                        View parentRow = (View) buttonView.getParent();
                        ListView listView = (ListView) parentRow.getParent();
                         positiono = listView.getPositionForView(parentRow);
                        mPlayingPosition = positiono;

                        final Runnable mUpdateTime = new Runnable() {
                            public void run() {
                                int currentDuration;
                                int totalduration;
                                if (mPlayer.isPlaying()) {
                                    currentDuration = mPlayer.getCurrentPosition();
                                    totalduration = mPlayer.getDuration();
                                    textview_duration_MAX.setText("" + milliSecondsToTimer((long) totalduration));
                                    textview_duration_Current.setText("" + milliSecondsToTimer((long) currentDuration));
                                    textview_duration_Current.postDelayed(this, 1000);
                                } else {
                                    textview_duration_Current.removeCallbacks(this);
                                }
                            }
                        };

                        isStart = false;
                        //   Toast.makeText(getContext(), "Play", Toast.LENGTH_SHORT).show();
                        Toast.makeText(getContext(),"positiono"+positiono,Toast.LENGTH_SHORT).show();
                        Log.d("OnlyPosssssd", String.valueOf(mPlayingPosition));


                        try {
                            mPlayer.reset();
                            mPlayer.setDataSource(String.valueOf(planet = listAdapter.getItem(mPlayingPosition)));

                            mPlayer.prepare();
                            mPlayer.start();
                            mPlayingPosition = positiono;
                            // Log.d("OnlyPos", String.valueOf(mPlayingPosition));


                            pb.setProgress(0);
                            pb.setMax(100);


                            seekbarf.setMax(mPlayer.getDuration());

                            updatePosition();

                            isStarted = true;
                            mHandler.postDelayed(mProgressUpdater, 500);

                        } catch (IOException e) {

                            e.printStackTrace();

                        }
                      );}


            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show();
                    itemPosition = itemPosition+1;
                    next(itemPosition);
                    playSong(itemPosition);
                    if (view != null) {
                        CheckBox checkBox = (CheckBox)view.findViewById(R.id.CheckBox01);
                        checkBox.setChecked(!checkBox.isChecked());

                    }
                }
            });

    public  void next(int next){

    Toast.makeText(getContext(),"Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG).show();
 next, Toast.LENGTH_SHORT).show();

    parents.getChildAt(next).setBackground(getContext().getDrawable(R.drawable.backpause));

    if (save != -1 && save != next){
        parents.getChildAt(save).setBackgroundColor(Color.TRANSPARENT);      
    }
    save = next;

}

1 个答案:

答案 0 :(得分:0)

由于您已经在Planet类中使用了setChecked方法,因此最好是保留最后一次单击索引的轨迹,并在每个下一个按钮上单击增加它。然后根据getChecked()结果从该索引的项目列表中获取行星项目,setChecked(true)或false,然后调用adapter.notifydatasetchanged。

相关问题