5

Tôi đang cố gắng truy cập danh sách hoạt động bằng cách sử dụng bộ điều hợp tùy chỉnh. Tôi đã thử trực tiếp mà không cần sử dụng bất kỳ bộ điều hợp tùy chỉnh nào hoạt động tốt nhưng tôi muốn thêm nhiều chức năng hơn vào danh sách xem. tùy chỉnh adapter.Now Tôi đã thử nó nhưng tôi nhận được một danh sách trống xem mà không có dữ liệu có thể nhìn thấy. List-Hoạt độngSử dụng tùy chỉnh simpleCursorAdapter

public class MainActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 

     String[] projection = { 
        MediaStore.Audio.Media._ID, 
        MediaStore.Audio.Media.ARTIST, 
        MediaStore.Audio.Media.TITLE, 
        MediaStore.Audio.Media.DATA, 
        MediaStore.Audio.Media.DISPLAY_NAME, 
        MediaStore.Audio.Media.DURATION, 

      }; 
      //query 

      musiccursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,sortOrder); 
      music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 


      int a[]= new int[]{R.id.TitleSong,R.id.Artist}; 


      Custom_Adapter adapter = new Custom_Adapter(this,R.layout.music_items, musiccursor, new String[]{MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST} ,a); 

      this.setAdapter(adapter); 

      } 
    } 

Custom-Adaptor

public class Custom_Adapter extends SimpleCursorAdapter { 


    private Context mContext; 
    private Context appContext; 
    private int layout; 
    private Cursor cr; 
    private final LayoutInflater inflater; 

    public Custom_Adapter(Context context,int layout, Cursor c,String[] from,int[] to) { 
     super(context,layout,c,from,to); 
     this.layout=layout; 
     this.mContext = context; 
     this.inflater=LayoutInflater.from(context); 
     this.cr=c; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // TODO Auto-generated method stub 
     super.bindView(view, context, cursor); 
    view=inflater.inflate(layout, null, false); 
     TextView titleS=(TextView)view.findViewById(R.id.TitleSong); 
     TextView artistS=(TextView)view.findViewById(R.id.Artist); 
     int Title_index; 
     int Artist_index; 
     cursor.moveToFirst(); 
     while(cursor.isLast()){ 

      Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
      Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 
      titleS.setText(cursor.getString(Title_index)); 
      artistS.setText(cursor.getString(Artist_index)); 
      cr.moveToNext(); 
      } 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     return convertView; 
    } 
} 

Trả lời

13

Khi mở rộng một bộ chuyển đổi con trỏ bạn nên ghi đè lên các phương pháp bindViewnewView. Phương thức bindView được sử dụng để liên kết tất cả dữ liệu với một khung nhìn cụ thể như thiết lập văn bản trên một TextView. Phương thức newView được sử dụng để thổi phồng một khung nhìn mới và trả lại nó, bạn không ràng buộc bất kỳ dữ liệu nào với khung nhìn tại thời điểm này. Hầu hết các bộ điều hợp sử dụng chức năng getView nhưng khi mở rộng một bộ điều hợp con trỏ, bạn nên sử dụng bindView và newView.

public class Custom_Adapter extends SimpleCursorAdapter { 

      private Context mContext; 
      private Context appContext; 
      private int layout; 
      private Cursor cr; 
      private final LayoutInflater inflater; 

      public Custom_Adapter(Context context,int layout, Cursor c,String[] from,int[] to) { 
       super(context,layout,c,from,to); 
       this.layout=layout; 
       this.mContext = context; 
       this.inflater=LayoutInflater.from(context); 
       this.cr=c; 
      } 

      @Override 
      public View newView (Context context, Cursor cursor, ViewGroup parent) { 
        return inflater.inflate(layout, null); 
      } 

      @Override 
      public void bindView(View view, Context context, Cursor cursor) { 
       super.bindView(view, context, cursor); 
       TextView titleS=(TextView)view.findViewById(R.id.TitleSong); 
       TextView artistS=(TextView)view.findViewById(R.id.Artist); 

       int Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
       int Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 

       titleS.setText(cursor.getString(Title_index)); 
       artistS.setText(cursor.getString(Artist_index)); 

      } 

    } 
+0

Tôi có thể nhận dữ liệu ở chế độ xem danh sách nhưng chỉ dữ liệu hàng đầu tiên xuất hiện trong tất cả các hàng.Kiểm tra câu hỏi được cập nhật của tôi. –

+0

Tôi hơi bối rối những gì bạn thay đổi nhưng bạn nên chuyển sang hai phương pháp tôi đã đề cập ở trên. Dù bằng cách nào tôi nghĩ rằng vấn đề bạn đang gặp phải là bạn không di chuyển chỉ mục con trỏ. Bạn nên gọi cr.moveToPosition (position) trước khi bạn kéo dữ liệu ra khỏi con trỏ. Chuyển đổi các phương thức newView bindView để giảm nhu cầu tăng lượt xem mới khi bạn không kiểm tra xem convertView có rỗng hay không và sử dụng lại nó. – Bobbake4

+0

Bạn cũng đang in chỉ mục của các cột không phải là dữ liệu thực được lưu trữ trong con trỏ. – Bobbake4

1

bạn viết tốt hơn những dòng mã trong customadaptergetview chức năng của bạn. Nó làm việc cho tôi và sẽ làm việc cho bạn, nó đơn giản.

if (convertView == null) { 
    music_column_index = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
    myCursor.moveToPosition(position); 
    id = myCursor.getString(music_column_index); 
    music_column_index = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE); 
    myCursor.moveToPosition(position); 
    id += " Size(KB):" + myCursor.getString(music_column_index); 
    Log.d("TAG", "id::" + id); 
    tv.setText(id); 
} else 
    tv = (TextView) convertView; 
    return tv; 
} 
+0

Mmm, tôi không thể thấy giải pháp này sạch như thế nào –

Các vấn đề liên quan