2013-05-28 67 views
2

我正在使用BaseAdapter的getView()方法動態檢索所有音頻文件(使用專輯封面)的應用程序。使用列表視圖在BaseAdapter中刪除ConvertView運行時間

現在我該如何刪除listView上的項目。 (例如,如果我點擊第三項listView,第三項應該刪除或刪除)。

music_cursor代碼

String[] proj = { MediaStore.Audio.Media._ID, 
     MediaStore.Audio.Media.DATA, 
     MediaStore.Audio.Media.DISPLAY_NAME, 
     MediaStore.Video.Media.SIZE, MediaStore.Audio.Media.ARTIST, 
     MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.TITLE }; 

Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
     proj, null, null, null); 
int count = musiccursor.getCount(); 

適配器代碼:

public class MusicAdapter extends BaseAdapter { 
     private Context mContext; 

     public MusicAdapter(Context con) { 
      mContext = con; 
     } 
     public int getCount() { 
      return count; 
     } 
     public Object getItem(int position) { 
      return position; 
     } 
     public long getItemId(int position) { 
      return position; 
     } 

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

      String id = null; 
      ViewHolder holder; 
      LayoutInflater inflater = getLayoutInflater(); 

      if (convertView == null) { 

       convertView = inflater.inflate(R.layout.sub_layout, null); 
       holder = new ViewHolder(); 

       holder.ID_ = (TextView) convertView.findViewById(R.id.txtSongName); 
       holder.SIZE_ = (TextView) convertView.findViewById(R.id.txtSongSize); 
       holder.ARTIST = (TextView) convertView.findViewById(R.id.txtSongArtist); 
       holder.TITLE_image = (ImageView) convertView.findViewById(R.id.imgArtWork); 

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

      // code for retrieving audio name and set to textView 
      music_column_index = musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
      musiccursor.moveToPosition(position); 
      id = musiccursor.getString(music_column_index); 
      holder.ID_.setText(id); 
      holder.ID_.setTextSize(15); 

      // code for retrieving audioSize and set to textView 
      music_column_index = musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE); 
      musiccursor.moveToPosition(position); 
      id = (musiccursor.getString(music_column_index) + " (KB)"); 
      holder.SIZE_.setText(id); 
      holder.SIZE_.setTextSize(15); 

      // code for retrieving audio_Artist name and set to textView 
      music_column_index = musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 
      musiccursor.moveToPosition(position); 
      id = musiccursor.getString(music_column_index); 
      holder.ARTIST.setText(id); 
      holder.ARTIST.setTextSize(15); 

      // code for retrieving album-art(from albumid) and set to ImageView 

      musiccursor.moveToPosition(position); 
      album_id_long = musiccursor.getLong(musiccursor 
        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)); 

      Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
      Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id_long); 
      ContentResolver res = MusicActivity.this.getContentResolver(); 
      InputStream in = null; 
      try { 
       in = res.openInputStream(uri); 
      } catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } 
      Bitmap artwork = BitmapFactory.decodeStream(in); 

      holder.TITLE_image.setImageBitmap(artwork); 

      convertView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        //on this code of onClick,a selected item should be deleted 

       } 
      }); 

      return convertView; 
     } 

     private class ViewHolder { 
      TextView ID_; 
      TextView SIZE_; 
      TextView ARTIST; 
      ImageView TITLE_image; 
     } 
    } 

enter image description here

如果有另一種方式來做到這一點。 ,將不勝感激。

謝謝。

回答

1

你必須覆蓋

public void onListItemClick(ListView l, View v, int position, long id) 
當你點擊一個視圖

,這個回調函數會被調用。當它被調用時,您可以從數據庫中刪除條目,然後重新運行您創建的查詢來填充您的ListView。如果您exteding Activity而不是ListActivity,你必須設置OnItemClickListener

yourListView.setOnItemClickListener(
    new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, 
       int position, long id) { 


      } 
     } 
); 
+1

你能告訴我什麼是錯? ? – Blackbelt

+0

我沒有那樣做@blackbelt – Rumit

+0

我沒有指責你 – Blackbelt

1

你確實要刪除音樂文件或簡單地從ListView中刪除嗎? 順便說一句,你只需要撥打 「musiccursor.moveToPosition(position);」 曾經在開始

+0

實際上我想要兩個操作。但爲此,首先我必須從listView first.i中刪除項目也能夠通過修復arrayAdapter ,但現在文件來自getViw區域,無法處理刪除操作。 – Rumit

相關問題