質問編集履歴

2

ArrayAdapter<String>について、追記

2016/08/01 12:05

投稿

js.making
js.making

スコア23

test CHANGED
File without changes
test CHANGED
@@ -252,6 +252,14 @@
252
252
 
253
253
  ```
254
254
 
255
+ /*
256
+
257
+ @param ArrayAdapter<String> songList; ContentResolverからデータを読み取り、ListFragmentのListViewへ出力するArrayAdapter
258
+
259
+ */
260
+
261
+
262
+
255
263
  //MainfragmentのMUSICボタンから起動した時の操作
256
264
 
257
265
  if (extension.equals(".mp3")) { //音楽データを取得しListFragmentへ出力

1

追加でコードを入力

2016/08/01 12:05

投稿

js.making
js.making

スコア23

test CHANGED
File without changes
test CHANGED
@@ -243,3 +243,183 @@
243
243
  どちらもandroidBeamには対応しているはずですが、
244
244
 
245
245
  端末に起因することもあるのでしょうか?
246
+
247
+
248
+
249
+ ###追加したコード(Adapter部分)
250
+
251
+
252
+
253
+ ```
254
+
255
+ //MainfragmentのMUSICボタンから起動した時の操作
256
+
257
+ if (extension.equals(".mp3")) { //音楽データを取得しListFragmentへ出力
258
+
259
+
260
+
261
+ columns = new String[]{
262
+
263
+ MediaStore.Audio.Media._ID,
264
+
265
+ MediaStore.Audio.Media.ARTIST,
266
+
267
+ MediaStore.Audio.Media.ALBUM,
268
+
269
+ MediaStore.Audio.Media.DURATION,
270
+
271
+ MediaStore.Audio.Media.TRACK,
272
+
273
+ MediaStore.Audio.Media.TITLE
274
+
275
+ };
276
+
277
+
278
+
279
+
280
+
281
+ ContentResolver cr = getActivity().getContentResolver();
282
+
283
+
284
+
285
+ Cursor cursor = cr.query(
286
+
287
+ MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
288
+
289
+ columns,
290
+
291
+ null,
292
+
293
+ null,
294
+
295
+ null
296
+
297
+ );
298
+
299
+ if (cursor == null) throw new AssertionError();
300
+
301
+ if (cursor != null) cursor.moveToFirst();
302
+
303
+ songList = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
304
+
305
+ do {
306
+
307
+ //リストに名前を追加
308
+
309
+ songList.add(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
310
+
311
+
312
+
313
+
314
+
315
+ } while (cursor.moveToNext());
316
+
317
+
318
+
319
+ setListAdapter(songList);
320
+
321
+ cursor.close();
322
+
323
+
324
+
325
+
326
+
327
+ //****************
328
+
329
+ }else if(extension.equals(".pdf")){
330
+
331
+ //以下はpdfデータをListFragmentへ出力
332
+
333
+
334
+
335
+ columns = new String[]{ //query第2関数
336
+
337
+ MediaStore.Files.FileColumns.TITLE,
338
+
339
+ MediaStore.Files.FileColumns.MIME_TYPE,
340
+
341
+ MediaStore.Files.FileColumns.DATA
342
+
343
+ };
344
+
345
+
346
+
347
+
348
+
349
+ ContentResolver cr = getActivity().getContentResolver();
350
+
351
+ Uri uri = MediaStore.Files.getContentUri("external"); //query第1関数
352
+
353
+
354
+
355
+ //query第3関数 selectionにMIME_TYPEを指定
356
+
357
+ String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
358
+
359
+
360
+
361
+ //query第4関数 selectionArgsを指定
362
+
363
+ String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
364
+
365
+ String[] selectionArgsPdf = new String[]{mimeType};
366
+
367
+
368
+
369
+ //query第5関数 sortOrderを指定 今回は指定なしの為、null
370
+
371
+ String sortOrder = null;
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+ Cursor cursor = cr.query(
380
+
381
+ uri,
382
+
383
+ columns,
384
+
385
+ selectionMimeType,
386
+
387
+ selectionArgsPdf,
388
+
389
+ sortOrder
390
+
391
+ );
392
+
393
+ if (cursor == null) throw new AssertionError();
394
+
395
+ if (cursor != null) cursor.moveToFirst();
396
+
397
+ songList = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
398
+
399
+ do {
400
+
401
+ //リストに名前を追加
402
+
403
+ songList.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE)));
404
+
405
+ Log.i("TAG", "TITLE: " + MediaStore.Files.FileColumns.TITLE + ":odf");
406
+
407
+ Log.i("TAG", "MIMETYPE: " + MediaStore.Files.FileColumns.MIME_TYPE + ":pdf");
408
+
409
+ Log.i("TAG", "DATA: " + MediaStore.Files.FileColumns.DATA + "::pdf");
410
+
411
+
412
+
413
+ } while (cursor.moveToNext());
414
+
415
+
416
+
417
+ setListAdapter(songList);
418
+
419
+ cursor.close();
420
+
421
+
422
+
423
+ }
424
+
425
+ ```