如何在android中获取选中复选框的值并将其放入字符串[]数组中

如何在android中获取选中复选框的值并将其放入字符串[]数组中,android,arrays,android-checkbox,Android,Arrays,Android Checkbox,我正在创建此应用程序,其中列出了我手机中的所有音乐,并带有复选框和按钮。如果我选中列表中的一个项目并单击按钮,它将压缩所选项目。我的问题是,我需要获取所选项目的值(目录路径),并将其放入字符串[]数组中。我试图寻找这个,但我感到困惑。我是新来的android开发人员。请帮帮我 这就是我到目前为止所做的 Music.java public class Music extends AppCompatActivity { Spinner spinneruse; ListView listView;

我正在创建此应用程序,其中列出了我手机中的所有音乐,并带有复选框和按钮。如果我选中列表中的一个项目并单击按钮,它将压缩所选项目。我的问题是,我需要获取所选项目的值(目录路径),并将其放入字符串[]数组中。我试图寻找这个,但我感到困惑。我是新来的android开发人员。请帮帮我

这就是我到目前为止所做的

Music.java

public class Music extends AppCompatActivity {

Spinner spinneruse;
ListView listView;
SongAdapter songAdapter;
Button getChoice;
ArrayList<SongInfo> _songs = new ArrayList<SongInfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    listView = (ListView) findViewById(R.id.lv);
    getChoice = (Button) findViewById(R.id.btnMusic);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                SongInfo s = new SongInfo(name, artist, url);
                _songs.add(s);

            } while (cursor.moveToNext());

        }

        cursor.close();
        songAdapter = new SongAdapter(Music.this, _songs);

    }
    listView.setAdapter(new SongAdapter(this, _songs));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            songAdapter.setSelected(position);
        }
    });
    getChoice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // declare an array for storing the files i.e the path
            // of your source files


            // first parameter is d files second parameter is zip
            // file name
            Compress c = new Compress(songs, "/storage/sdcard0/file.zip");   //first parameter is d files second parameter is zip file name
            c.zip();

        }

    });
}//oncreate

static class SongInfo {
    public String Songname;
    private String Artistname;
    private String SongUrl;
    private boolean isSelected;


    public SongInfo(String songname, String artistname, String songUrl) {
        Songname = songname;
        Artistname = artistname;
        SongUrl = songUrl;
        this.isSelected = isSelected;
    }

    public String getSongname() {
        return Songname;
    }

    public void setSongname(String songname) {

        Songname = songname;
    }

    public String getArtistname() {
        return Artistname;
    }

    public void setArtistname(String artistname) {
        Artistname = artistname;
    }

    public String getSongUrl() {
        return SongUrl;
    }

    public void setSongUrl(String songUrl) {

        SongUrl = songUrl;
    }


    public boolean isSelected() {

        return isSelected;
    }

    public void setSelected(boolean selected) {

        isSelected = selected;
    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class SongAdapter extends BaseAdapter {
    public ArrayList<SongInfo> _songs = new ArrayList<SongInfo>();
    private Context context;
    LayoutInflater inflater;

    public SongAdapter(Context _context, ArrayList<SongInfo> songs) {
        this.context = _context;
        this._songs = songs;
    }

    public int getCount() {
        return _songs.size();
    }

    public Object getItem(int position) {
        return _songs.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {

        protected CheckBox checkBox;
        private TextView tvSongName;
        private TextView tvSongArtist;


    }

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


        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_songs, null, true);

            viewHolder = new ViewHolder();

            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
            viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);
            viewHolder.checkBox.setOnCheckedChangeListener(null);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.tvSongName.setText(_songs.get(position).getSongname());
        viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());

        viewHolder.checkBox.setOnCheckedChangeListener(null);
        viewHolder.checkBox.setChecked(_songs.get(position).isSelected());

        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.

                if (_songs.get(getPosition).isSelected()) {
                    _songs.get(getPosition).setSelected(false);

                } else {
                    _songs.get(getPosition).setSelected(true);
                    Toast.makeText(context.getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
                            Toast.LENGTH_LONG).show();
                    Log.e("URL", _songs.get(position).getSongUrl() + " ");
                }


            }


        });
        convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
        convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
        convertView.setTag(R.id.checkBox, viewHolder.checkBox);

        viewHolder.checkBox.setTag(position); // This line is important.


        return convertView;
    }

    public ArrayList<SongInfo> getAllData() {
        return _songs;
    }

    public void setSelected(int position) {
        //Update status of checkbox
        SongInfo items = _songs.get(position);
        items.setSelected(!items.isSelected());
        notifyDataSetChanged();
    }

}
}
公共课堂音乐活动{
纺纱机;
列表视图列表视图;
歌曲适配器;
按钮选择;
ArrayList_songs=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
listView=(listView)findViewById(R.id.lv);
getChoice=(按钮)findViewById(R.id.btnMusic);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Uri Uri=MediaStore.Audio.Media.EXTERNAL\u CONTENT\u Uri;
字符串选择=MediaStore.Audio.Media.IS_MUSIC+“!=0”;
Cursor Cursor=getContentResolver().query(uri,null,selection,null,null);
如果(光标!=null){
if(cursor.moveToFirst()){
做{
字符串名称=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_name));
stringartist=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.artist));
字符串url=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
SongInfo s=新的SongInfo(姓名、艺术家、url);
_歌曲。添加(s);
}while(cursor.moveToNext());
}
cursor.close();
songAdapter=新的songAdapter(Music.this,_歌曲);
}
setAdapter(新歌曲适配器(这个,_歌曲));
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
songAdapter.setSelected(位置);
}
});
getChoice.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//声明用于存储文件的数组,即路径
//您的源文件的
//第一个参数是d文件,第二个参数是zip文件
//文件名
Compress c=new Compress(歌曲,“/storage/sdcard0/file.zip”);//第一个参数是d files,第二个参数是zip文件名
c、 zip();
}
});
}//一次创建
静态类SongInfo{
公共字符串词名;
私人字符串艺术家姓名;
私有字符串;
私人选举;
公共歌曲信息(字符串歌曲名、字符串艺术家名、字符串歌曲URL){
Songname=Songname;
艺人名称=艺人名称;
SongUrl=SongUrl;
this.isSelected=isSelected;
}
公共字符串getSongname(){
返回歌曲名;
}
public void setSongname(字符串songname){
Songname=Songname;
}
公共字符串getArtistname(){
返回艺人姓名;
}
public void setArtistname(字符串artistname){
艺人名称=艺人名称;
}
公共字符串getSongUrl(){
返回SongUrl;
}
公共void setSongUrl(字符串songUrl){
SongUrl=SongUrl;
}
公选{
选举产生的回报;
}
已选择公共无效设置(已选择布尔值){
isSelected=已选择;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
类SongAdapter扩展了BaseAdapter{
public ArrayList_songs=new ArrayList();
私人语境;
充气机;
公共歌曲适配器(上下文_上下文,ArrayList歌曲){
this.context=\u context;
这个。_歌曲=歌曲;
}
public int getCount(){
返回_songs.size();
}
公共对象getItem(int位置){
返回歌曲。获取(位置);
}
公共长getItemId(int位置){
返回位置;
}
私有类视窗持有者{
受保护复选框;
私有文本视图tvSongName;
私人TextView tvSongArtist;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder ViewHolder=null;
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=充气机。充气(R.layout.row_,null,true);
viewHolder=新的viewHolder();
viewHolder.checkBox=(checkBox)convertView.findViewById(R.id.checkBox);
viewHolder.tvSongName=(TextView)convertView.findViewById(R.id.tvSongName);
viewHolder.tvSongArtist=(TextView)convertView.findViewById(R.id.tvatistName);
viewHolder.checkBox.setOnCheckedChangeListener(null);
convertView.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)convertView.getTag();
}
viewHolder.tvSongName.setText(_songs.get(position.getSongname());
viewHolder.tvSongArtist.setText(_songs.get(position.getArtistname());
viewHolder.checkBox.setOnCheckedChangeListener(null);
viewHolder.checkBox.setChecked(_songs.get(position.isSelected());
viewHolder.checkBox.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
int getPosition=(Integer)buttonView.getTag();//这里我们使用s获取为复选框设置的位置
@Override
public void onClick(View v) {
// declare an array for storing the files i.e the path
// of your source files

// step 1: put all the selected songs into a ArrayList (songsSelected)
ArrayList<SongInfo> songsSelected = new ArrayList<>();
for(SongInfo songInfo : songAdapter._songs) {
    if (songInfo.isSelected()) {
        songsSelected.add(songInfo);
    }
}

// step 2: new a string array, save the Uri info into the array
String filesSelected[] = new String[songsSelected.size()];
for (int i=0; i<songsSelected.size(); i++) {
    filesSelected[i] = songsSelected.get(i).getSongUrl();
}

// first parameter is d files second parameter is zip
// file name
Compress c = new Compress(songs, "/storage/sdcard0/file.zip");   //first parameter is d files second parameter is zip file name
c.zip();