设备上可用的Android ListView视频

时间:2013-12-31 12:45:43

标签: android listview android-contentprovider mediastore

以下代码无法正常运行,应用程序在启动时因“不幸......”错误而关闭。

protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_media_store_list_view);

    vedio_list = (ListView) findViewById(R.id.vedio_list);

    Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

    String[] projection = { MediaStore.Video.VideoColumns.DISPLAY_NAME};

    cursor = getContentResolver().query(contentUri, projection, null, null, null);

    title_index = cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.TITLE);

    al = new ArrayList<String>();

    showList();
   }

private void showList() {

    while(cursor.moveToNext())
    {
        String title = cursor.getString(title_index);

        al.add(0 , title);
        aa.notifyDataSetChanged();
    }
    cursor.close();

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

通过获取计数并移动到列表中的第一个来检查光标是否良好:

private void showList() {
if(cursor.getCount() > 0){
   cursor.moveToFirst();
   while(cursor.moveToNext())
     {
    String title = cursor.getString(title_index);
    al.add(0 , title);
    aa.notifyDataSetChanged();
     }
 }
cursor.close();
}