1

我有两个字符串数组:

String[] name ={ “Sound 1″, “Soundname 2″, “Chainsaw!”};
int[] sounds = {R.raw.sound1, R.raw.sound2, R.raw.chainsaw};

和一个列表视图:

list.setOnItemClickListener(new OnItemClickListener() {

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

         switch(position) {


            case 0: if(isplaying){audio.stop();} audio = MediaPlayer.create(context, R.raw.sound1); playSound(); 
                    break;

            case 1: if(isplaying){audio.stop();} audio = MediaPlayer.create(context, R.raw.sound2); playSound(); 
                    break;

            case 2: if(isplaying){audio.stop();} audio = MediaPlayer.create(context, R.raw.sound3); playSound(); 
                    break;

            case 3: if(isplaying){audio.stop();} audio = MediaPlayer.create(context, R.raw.sound4); playSound(); 
                    break;
                         }

           }

     });

我使用此功能将资源声音文件设置为通知:

 public boolean saveas(int ressound){  
 byte[] buffer=null;  
 InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
 int size=0;  

 try {  
  size = fIn.available();  
  buffer = new byte[size];  
  fIn.read(buffer);  
  fIn.close();  
 } catch (IOException e) {  
  // TODO Auto-generated catch block  
  return false;  
 }  

 String path="/sdcard/media/audio/ringtones/";  
 String filename="examplefile"+".ogg";  

 boolean exists = (new File(path)).exists();  
 if (!exists){new File(path).mkdirs();}  

 FileOutputStream save;  
 try {  
  save = new FileOutputStream(path+filename);  
  save.write(buffer);  
  save.flush();  
  save.close();  
 } catch (FileNotFoundException e) {  
     Toast.makeText(this, "ERRORE", Toast.LENGTH_SHORT).show();  
  return false;  
 } catch (IOException e) {  
     Toast.makeText(this, "ERRORE", Toast.LENGTH_SHORT).show(); 
  return false;  
 }      

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

 File k = new File(path, filename);  

 ContentValues values = new ContentValues();  
 values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
 values.put(MediaStore.MediaColumns.TITLE, "exampletitle");  
 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
 values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
 values.put(MediaStore.Audio.Media.IS_ALARM, true);  
 values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

 //Insert it into the database  
 this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

 return true;  
} 

此函数由上下文菜单调用,该菜单在长按列表视图的项目后显示。该函数将像这样调用:

 saveas(R.raw.soundfile);  

我的问题是:如何通过单击列表视图的哪个项目来获取不同的 id 或 R.raw.soundfile 到 saveas 函数?

4

0 回答 0