2017-12-27 23 views
-1

목록 배열로 내 recycler 뷰를 채우고 있습니다. String Array를 시도해 보았습니다. 모든 일을 잘못하고 있거나 arrayAdapter를 사용해야합니까?RecyclerView를 ListArray로 채우기

배열 어댑터를 사용해야한다면 어떻게해야합니까? 나는 이미 arrayadapter로 listview를 채웠다. 그러나 나는 바다 한가운데에 큰 것을 시도하고 싶지 않다. 내 recyclerview 어댑터 코드는 다음과 같습니다.

오류가 표시되지 않습니다. 뷰는 배열의 마지막 항목으로 채워집니다.

public class AudioRecycleViewAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> { 
private List<String> values = Arrays.asList ("Al Feel","Al Humazah","Al 
Kausar","Al Maoon","Al Quresh","Dr Israr", 
     "Al Kafiroon","An Nasr","Al Lahab","Al Ikhlaas","Al Falaq","An 
Naas","Iman Part 1", 
     "Iman Part 2","Iman Part 3","Iman Part 4","Iman Part 5"); 

Context context; 
InterstitialAd mInterstitialAd; 
private int audiolayout; 
public TextView txtHeader; 
public TextView txtFooter; 
TextView txtarabic; 


public AudioRecycleViewAdapter(List<String> values, int audiolayout, Context context, InterstitialAd intAdView) throws IOException { 
    this.audiolayout = audiolayout; 
    this.context = context; 
    this.mInterstitialAd = intAdView; 
} 
public Context getContext() { 
    context = this.getContext(); 
    return context; 
} 

// Provide a reference to the views for each data item 
// Complex data items may need more than one view per item, and 
// you provide access to all the views for a data item in a view holder 
public class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public View layout; 

    public ViewHolder(View v) { 
     super(v); 
     layout = v; 
     txtHeader = (TextView) v.findViewById(R.id.audio_subtitle); 
     txtFooter = (TextView) v.findViewById(R.id.audio_title); 
     txtarabic = (TextView) v.findViewById(R.id.arabic_title); 
    } 
} 

public void add(int position, String item) { 
    values.add(position, item); 
    notifyItemInserted(position); 
} 

public void remove(int position) { 
    values.remove(position); 
    notifyItemRemoved(position); 
} 
// Create new views (invoked by the layout manager) 
@Override 
public AudioRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
               int viewType) { 
    // create a new view 
    LayoutInflater inflater = LayoutInflater.from(
      parent.getContext()); 

    View v = 
      inflater.inflate(R.layout.audio_row_layout, parent, false); 
    // set the view's size, margins, paddings and layout parameters 
    ViewHolder vh = new ViewHolder(v); 

    return vh; 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    int x; 
    for(position =0; position < values.size(); position++) 
    { 
     txtHeader.setText(values.get(position)); 
     position++; 
    } 

} 

// Return the size of your dataset (invoked by the layout manager) 
@Override 
public int getItemCount() { 
    return values.size(); 
} 

} 
+0

오류 로그를 공유 할 수 있습니까? – R2R

+0

가 오류를 표시하지 않습니다 ... 모든 행에 대해 목록의 마지막 항목을 채우는 중 –

답변

1

첫째, 그래서이

extends RecyclerView.Adapter<AudioRecycleViewAdapter.ViewHolder> 

두 번째로이

extends RecyclerView.Adapter<RecyclerView.ViewHolder> 

을 변경 AudioRecycleViewAdapter 내부에 사용자 정의 ViewHolder를 사용하여 당신은 단지 textHeader 마지막 ​​값을 표시, 모든 요소를 ​​통해거야 또한 사용하십시오 holder.txtHeader

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     holder.txtHeader.setText(values.get(position)); 

} 
,

셋째 항목 레이아웃 뷰의 아이 뷰는, 그래서

public class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public View layout; 
    public TextView txtHeader; 
    public TextView txtFooter; 
    public TextView txtarabic; 

    public ViewHolder(View v) { 
     super(v); 
     layout = v; 
     txtHeader = (TextView) v.findViewById(R.id.audio_subtitle); 
     txtFooter = (TextView) v.findViewById(R.id.audio_title); 
     txtarabic = (TextView) v.findViewById(R.id.arabic_title); 
    } 
} 
+0

이미 시도했습니다. 메소드가 수퍼 클래스 오류로 덮어 쓰지 않습니다. –

+0

@ManzoorAhmad 예, 오래된 메소드 서명으로 인해 이전 메소드를 제거하고 스튜디오가 새로운 시그니처를 가진 메소드를 오버라이드하면,'AudioRecycleViewAdapter.ViewHolder'가'onBindViewHolder'의 param으로 보일 것입니다. –

+0

좋아요. 바꿀 수 없어요., –

0

Pavneet_Singh의 대답 이외에, 당신은 또한 RecyclerView 항목에 관련된 데이터를 사용자 정의 ViewHolder에 바인드() 메소드를 추가하고 결합 할 수 ViewHolder 내부에 있어야한다 이 방법에서. 상황이 더욱 복잡 해지면 간단한 프로젝트에서이 방법을 사용했습니다. 그것은 다음과 같이 작동합니다 :

//adapter's inner 
class SomeClassViewHolder(val view: View) extends RecyclerView.ViewHolder(view) { 
    //here can be view initializing, like 
    txtHeader = (TextView) view.findViewById(R.id.audio_subtitle); 

    void bind(int position){ 
     // all view binding logic goes here, for example: 
     txtHeader.setText("someText"); 
    } 
} 

//and then in adapter 
@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     holder.bind(values.get(position)); 
} 

편집 : 이것은 당신이, 또한 등 textView처럼 holder 년대 바인딩 속성을 찾는 미래에 당신이 할 수있는 행동 홀더의 항목 청취자의 문제를 방지하는 데 도움이됩니다 올바르게 사용한bind() 방법.