2017-07-29 2 views
1

私はこのアプリケーションを作成しています。リスト内の項目をチェックしてボタンをクリックすると、選択した項目が圧縮されます。私の問題は、私が選択した項目の値(ディレクトリパス)を取得し、それをString []配列に入れる必要があることです。私はこれを検索しようとしましたが、混乱しました。私はこのアンドロイドを開発するのが初めてです。私を助けてください。選択したチェックボックスの値を取得してandroidのString []配列に入れる方法

これまで私が行ったこと。

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

} 
} 

事前にありがとうございます!ほんとうにありがとう。

+0

助けが必要pls :( – asgardwin7

答えて

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

}

IコードにSTEP1及びSTEP2にコメント。
filesSelectedは曲の選択リストです。

+0

WOW MAN!IT WORKS!これは本当にありがとうございます!あなたは私の日を保存します! – asgardwin7

関連する問題