null/index-outof-bound 등과 관련하여 많은 예외를 처리했습니다. 그러나 이것이 if/else와 같이 가야 할 올바른 방법이 아니라고 생각합니다. 그래서 누구나 나를 안내 할 수 있습니다. 어떻게 onBindViewHolder의 모든 null 조건을 처리 할 수 있습니까?BindViewHolder의 Recycler-view에서 null 값을 처리하는 가장 좋은 방법은 무엇입니까?
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (holder instanceof CustDtlsHolder)
{
// >i am getting here lots of null values below
if(position < arr_custdetails.size())
{
String dateInString = arr_custdetails.get(position).getLastVisitDate();
Date date = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
date = formatter.parse(dateInString);
SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MMM/yyyy");
update_date = formatter2.format(date);
update_date = update_date.replace("/"," ");
} catch (ParseException e) {
e.printStackTrace();
} catch (NullPointerException e){
update_date="null";
}
//> same getting null values below as mention above.
((CustDtlsHolder) holder).txt_mbr_date.setText(update_date.substring(0,2));
((CustDtlsHolder) holder).txt_mbr_year.setText(update_date.substring(3,6)+ " "+update_date.substring(7,11));
((CustDtlsHolder) holder).txt_mbr_name.setText(arr_custdetails.get(position).getFullName());
((CustDtlsHolder) holder).txt_mbr_email.setText(arr_custdetails.get(position).getEmailAddress());
((CustDtlsHolder) holder).txt_mbr_phone.setText(arr_custdetails.get(position).getMobileNumber());
((CustDtlsHolder) holder).txt_mbr_avgSize_val.setText(""+Math.round(arr_custdetails.get(position).getAvgBasketVal())+" , "+Math.round(arr_custdetails.get(position).getAvgBasketQty())+" "+"Units");
if(arr_custdetails.get(position).getLast90dayVisit() == null || arr_custdetails.get(position).getLast90dayVisit().equals(""))
{
((CustDtlsHolder) holder).txt_mbr_avgVisits_val.setText("");
}
else
{
((CustDtlsHolder) holder).txt_mbr_avgVisits_val.setText(arr_custdetails.get(position).getLast90dayVisit()+ " in Last 90 days");
}
((CustDtlsHolder) holder).txt_mbr_noofvisit.setText(arr_custdetails.get(position).getMbrVisitCount());
}
}
이 백분율 클래스에서 하나의 일반적인 방법을 만들어 Util.checkNull 같은 당신의 변수의 값을 전달 (arr_custdetails.get (위치) .getFullName()); true 또는 false를 반환하면 true를 반환하고 그렇지 않으면 false를 반환합니다. 그렇지 않으면 문자열에서 얻은 값을 설정합니다. – Dilip
ViewHolder 안에 함수를 추가하고 bind()를 호출하면 거기에서 코드를 조작하여 코드를 정리할 수 있습니다. –