2017-07-29 2 views
1

Ich erstelle diese App, die alle Musik in meinem Telefon mit Kontrollkästchen und Schaltfläche auflisten. wo in, wenn ich ein Element in der Liste überprüfen und auf die Schaltfläche klicken, werden die ausgewählten Elemente gezippt. Mein Problem ist für mich, dass ich den Wert (Verzeichnispfad) der ausgewählten Elemente abrufen und in ein Array String [] einfügen muss. Ich habe versucht, danach zu suchen, aber ich war verwirrt. Ich bin neu in diesem Android-Entwicklung. Bitte hilf mir.Wie bekomme ich den Wert des ausgewählten Kontrollkästchens und legte es in ein String [] Array in Android

das was ich bisher gemacht habe.

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

} 
} 

Vielen Dank im Voraus! Ich schätze es sehr.

+0

Brauchen Sie Hilfe pls :( – asgardwin7

Antwort

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 kommentiert Schritt 1 und Schritt 2 im Code.
filesSelected ist Lieder Dateiauswahlliste.
hoffe, Ihnen zu helfen

+0

WOW MAN! Es funktioniert! Vielen Dank FÜR DIESES! SIE SPAREN MEINEN TAG! – asgardwin7

Verwandte Themen