0

嗨,我有使用 Aquery 的复选框的歌曲列表视图,我无法在列表滚动时保存复选框的状态。我有两个arraylist,一个用于单击歌曲图像,另一个用于复选框,如果用户选择它应该根据复选框arraylist播放歌曲的复选框。我也用过这个 check.setChecked(SelectedBox.get(clickedposition).getisCheck()); . 但没有帮助

到目前为止我所做的是在这里

private void createList() {
        listAq = new AQuery(this);
        adapter = new ArrayAdapter<Audio>(SongList.this, R.layout.list_row,details) {
            @Override
            public  View getView(final int position, View convertView,final ViewGroup parent) {
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.list_row, parent, false);
                }
                final int clickedposition = position;
                final AQuery aq = listAq.recycle(convertView);
                Audio item = getItem(position);
                aq.id(R.id.MovieName).text(item.getAlbumName());
                aq.id(R.id.SongName).text(item.getTitle());
                aq.id(R.id.checkBox1).getCheckBox().setSelected(item.getisCheck());
                aq.id(R.id.nameLayout).clicked(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogBuilder(position);
                    }
                });
                 check = (CheckBox) convertView.findViewById(R.id.checkBox1);

                check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                        try {
                            if (isChecked) {
                                checkbox = true;
                                if (details.get(clickedposition).getisCheck() == false) {
                                SelectedBox.add(details.get(clickedposition));
                                System.out.println(SelectedBox.size());
                                details.get(clickedposition).setisCheck(true);
                                }
                            } else {
                                final String name = details.get(clickedposition).getFilename();
                    for (int i = 0; i <= SelectedBox.size(); i++) {
                    if (name.equals(SelectedBox.get(i).getFilename())) {
                                    SelectedBox.remove(i);
                                    details.get(clickedposition).setisCheck(false);

                                    }
                                }
                            }

                            notifyDataSetChanged();
                        }

                        catch (Exception e) {

                            e.getMessage();
                        }
                    }

                });
            try{

            }
            catch (Exception e) {
                // TODO: handle exception
            }
                url = "drawable/" + item.getThumbnail();
                imageResource = getResources().getIdentifier(url, null,getPackageName());
                aq.id(R.id.song_image).image(imageResource);
                if (position != playingValue) {
                    aq.id(R.id.imageplay).invisible();
                    aq.id(R.id.imageplay).getImageView().bringToFront();
                } else {
                    aq.id(R.id.imageplay).visible();
                }
                ImageView img = (ImageView)convertView.findViewById(R.id.song_image);
                img.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        checkbox = false;
                        SelectedBox.clear();
                        currentSongIndex = position;
                        try {
                            if (ChannelService.playerFlag == 0) {
                                playSong(currentSongIndex);
                            } else if ((ChannelService.playerFlag == 1)
                                    || ((ChannelService.playerFlag == 2))) {
                                if (currentSongIndex != playingValue) {
                                    try {
                                        final Intent serviceIntent = new Intent(
                                                getApplicationContext(),
                                                ChannelService.class);
                                        stopService(serviceIntent);
                                        mPlay.setBackgroundResource(R.drawable.play_btn);
                                        playSong(currentSongIndex);
                                    } catch (final IllegalStateException e) {
                                        e.printStackTrace();
                                    }
                                }
                                mPlay.setBackgroundResource(R.drawable.pause_btn);
                                final Intent in = new Intent("updateNotification");
                                sendBroadcast(in);
                            }
                        } catch (final IllegalStateException e) {
                            e.getMessage();
                        }
                        notifyDataSetChanged();
                    }
                });

                return convertView;

            }

        };

        aq.id(R.id.listView1).adapter(adapter);

    }
4

1 回答 1

0

当您在 Android 中向下滚动列表时,它会破坏不适合屏幕的行。我总是使用一个对象数组来保存每一行的状态,所以当 android 调用 getView 时,你可以询问它们的状态:

 public class AudioRow{
        public int  id;
        public boolean checked;
        ...
    }

然后在 getView 你可以做这样的事情:

public  View getView(final int position, View convertView,final ViewGroup parent) {
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.list_row, parent, false);
                }
                final int clickedposition = position;
                AudioRow ar = audioRows.get(position);
                if(ar.checked) {
                     //do something;
                }
                 ...                    

                    @Override
                    public void onClick(View v) {
                        if(ar.checked) ar.checked = false;
                        else ar.checked = true;
                        DialogBuilder(position);
                    }
                });

我希望它对你有帮助。

于 2013-05-10T06:13:45.567 回答