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

how to get the value of selected checkbox and put it in a String [ ] array in android

我正在创建这个应用程序,它列出了我 phone 中的所有音乐,上面带有复选框和按钮。如果我检查列表中的项目并单击按钮,它将压缩所选项目。我的问题是我需要获取所选项目的值(目录路径)并将其放入 String [ ] 数组中。我试图搜索这个,但我感到困惑。我是这个 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();
    }

}
}

提前致谢!非常感谢。

@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();

}

我在代码中对step1和step2进行了注释
filesSelected为歌曲文件选择列表。
希望对你有帮助