-1

내 재활용 코드 :recycler adapter viewholder textlistener의 모든 값을 계산 하시겠습니까?

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    @BindView(R.id.tvName) TextView tvName; 
    @BindView(R.id.tvRank) TextView tvRank; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     ButterKnife.bind(this, itemView); 
     itemView.setOnClickListener(this); 

    tvName.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       //blabla 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       //blabla 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       //blabla 
      } 
     }); 
} 

위 recycleview의 결과는 다음과 같이 될 것입니다 :

나는 텍스트 뷰 "순위"변화에있을 때 "순위"에서 모든 값을 계산하려는
----------------- 
Jack   10 
----------------- 
Jason   14 
----------------- 
Rony   11 
----------------- 

, 그것은 가능하니? 시도해 보았지만 모든 가치를 계산하는 방법은 하나의 가치 만 가질 수 있습니까? 그래서, 내가 원하는 결과는 당신이, 당신은 각 순위 값을 액세스하고 일부 INT 변수에 저장하는 추가하려는 RecyclerView의 어댑터 데이터 세트의 내부에 모든 값을 반복되어 수행 할 작업을

Jack, Rony, Jason = 35 
+1

더 자세히 설명해주세요. "모든 가치를 계산하고 싶다"라고했을 때 당신은 무엇을 의미 했습니까? –

+0

모든 값은 매우 모호합니다. 당신이 원하는 것을 정확히 설명 할 수 있다면 그것이 의미하는 바를 엄청나게 도움이 될 것입니다. 하지만 일부 수치를 계산하려고한다면 TextView에서 텍스트를 가져 와서 long, Integer, Double의 valueOf (...) 메서드를 사용하여 int 또는 long 또는 interger 유형으로 변환 할 수 있기를 바랍니다. , Float and Short. 그 개체의 각각은 또한 현재 데이터 형식을 다른 데이터 형식으로 변환하는 방법을 가지고 있습니다. 더 나아가, TextView에만 숫자가 들어 있는지 확인해야합니다. 그렇지 않으면 예외가 발생합니다. –

+0

@ MuratGuc 편집 질문 : – Akabaru

답변

0

입니다 전체. 합계를 모두 추가하면 값이 반환됩니다. 예 :

당신이 다음을 수행하여 INT로 변환 할 수있는 문자열 값으로 당신의 순위를 저장하는 경우
public class RankAdapter extends RecyclerView.Adapter { 

    public static class RankData { 
     public String name; 
     public int rank; 
    } 
    // This gets set either in a constructor or a setter method. 
    private ArrayList<RankData> rankDatas; 

    public int getTotalRank() { 
     int totalRank = 0; 
     for(RankData data : rankDatas){ 
      totalRank += data.rank; 
     } 
     return totalRank; 
    } 

    @Override 
    public RankViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     return new RankViewHolder(inflater.inflate(R.layout.checkable_item_layout_view, parent, false)); 
    } 

    @Override 
    public void onBindViewHolder(RankViewHolder holder, int position) { 
     RankData data = rankDatas.get(position); 
     holder.name.setText(data.name); 
     holder.rank.setText(data.rank); 
    } 

    public class RankViewHolder extends RecyclerView.ViewHolder { 
     public TextView name; 
     public TextView rank; 

     public RankViewHolder(View itemView) { 
      super(itemView); 
      name = itemView.findViewById(id.nameTxtVw); 
      rank = itemView.findViewById(id.rankTxtVw); 
     } 
    } 
} 

:

int rankInt = Integer.valueOf(rankStr).intValue(); 

을 그리고 당신은 다음에 rankInt를 추가 할 것 'totalRank'변수는 위와 같습니다.