2017-09-16 8 views
1

폴더에서 파일을 가져 와서 사용자 지정 어댑터를 사용하여 파일 이름을 기반으로 recyclerview를 채우려고합니다.폴더 내의 오디오 파일에서 사용자 정의 listview 채우기

onBindViewHolder 에서 :

Product m = dataList.get(position); 
    //title 
    holder.title.setText(m.getTitle()); 

과 :

void popList() { 
    Product product = new Product(); 
    File dir = new File(mainFolder);//path of files 
    File[] filelist = dir.listFiles(); 
    String[] nameOfFiles = new String[filelist.length]; 
    for (int i = 0; i < nameOfFiles.length; i++) { 
     nameOfFiles[i] = filelist[i].getName(); 
     product.setTitle(nameOfFiles[i]); 
    } 
    songList.add(product); 
} 

그러나 문제는 단지 첫 번째 항목을 추가한다

이 내가 그것을하고있어 방법이다. 나는 그것을 전부 추가하기 위해 어디에서 반복해야하는지 알아낼 수 없다.

답변

3

당신은 루프 항목에 대한 별도의 제품 객체를 생성하고 대신 마지막으로 설정 데이터를 개최한다 목록에서 하나의 Product 객체를 생성 목록에 추가 할 필요가

void popList() { 
    Product product ; 
    File dir = new File(mainFolder);//path of files 
    File[] filelist = dir.listFiles(); 
    String[] nameOfFiles = new String[filelist.length]; 
    for (int i = 0; i < nameOfFiles.length; i++) { 
     // create product 
     product = new Product(); 
     nameOfFiles[i] = filelist[i].getName(); 
     product.setTitle(nameOfFiles[i]); 
     // add it to list 
     songList.add(product); 
    } 
} 

귀하의 코드 도보

을 통해
void popList() { 
    Product product = new Product(); // one object 
    // ..code 
    for (int i = 0; i < nameOfFiles.length; i++) { 
     nameOfFiles[i] = filelist[i].getName(); 
     product.setTitle(nameOfFiles[i]); // at the end of loop set last file name to object 
    } 
    songList.add(product); // one object in the list , end of story 
}