我处理了很多关于null / index-outof-bound等的异常。但我认为这不是正确的方式去if if,所以任何人都可以指导我如何处理onBindViewHolder中的所有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());
}
}
答案 0 :(得分:1)
你可以用这种方式对你有所帮助......请试试这个。
<强>模型:强>
public class YourModelName{
private String mobileNumber="";
private String fullName="";
private String emailAddress="";
private String avgBasketVal="";
private String avgBasketQty="";
private String last90dayVisit="";
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getAvgBasketVal() {
return avgBasketVal;
}
public void setAvgBasketVal(String avgBasketVal) {
this.avgBasketVal = avgBasketVal;
}
public String getAvgBasketQty() {
return avgBasketQty;
}
public void setAvgBasketQty(String avgBasketQty) {
this.avgBasketQty = avgBasketQty;
}
public String getLast90dayVisit() {
return last90dayVisit;
}
public void setLast90dayVisit(String last90dayVisit) {
this.last90dayVisit = last90dayVisit;
}
}
答案 1 :(得分:1)
编辑@dilip回答:
public class YourModelName{
private String mobileNumber="";
private String fullName="";
private String emailAddress="";
private String avgBasketVal="";
private String avgBasketQty="";
private String last90dayVisit="";
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
if(mobileNumber != null)
this.mobileNumber = mobileNumber;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
if(fullName != null)
this.fullName = fullName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
if(emailAddress != null)
this.emailAddress = emailAddress;
}
public String getAvgBasketVal() {
return avgBasketVal;
}
public void setAvgBasketVal(String avgBasketVal) {
if(avgBasketVal != null)
this.avgBasketVal = avgBasketVal;
}
public String getAvgBasketQty() {
return avgBasketQty;
}
public void setAvgBasketQty(String avgBasketQty) {
if(avgBasketQty != null)
this.avgBasketQty = avgBasketQty;
}
public String getLast90dayVisit() {
return last90dayVisit;
}
public void setLast90dayVisit(String last90dayVisit) {
if(last90dayVisit != null)
this.last90dayVisit = last90dayVisit;
}
}
如果您的字符串值为null,则返回“”。
希望这可以帮助你。
答案 2 :(得分:1)
上面修改Model类的答案对我不起作用。
这就是我想出的
@Override
public void onBindViewHolder(HomeViewHolder holder, int position)
{
String title;
try {
title = myList.get(position).getTitle();
} catch (NullPointerException e) {
title = "Null";
}
holder.tvTitle.setText(title);
}
答案 3 :(得分:0)
对于字符串值,你可以使用android的TextUtils,你甚至可以在textView.setText()方法中设置null:
CustDtlsHolder custDtlsHolder = ((CustDtlsHolder) holder);
String value = arr_custdetails.get(position).getLast90dayVisit();
if (!TextUtils.isEmpty(value) {
value += " in Last 90 days";
}
custDtlsHolder.txt_mbr_avgVisits_val.setText(value);
但是正如已经写过的那样,你应该更好地创建绑定方法以获得更清晰的代码。通常在Java项目中我使用ButterKnife并创建一些这样的基类。
BaseRecyclerAdapter:
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
private List<T> items;
private OnRecyclerItemClickListener<T> onRecyclerItemClickListener;
public BaseRecyclerAdapter(List<T> items) {
this.items = items;
}
@Override public void onBindViewHolder(BaseViewHolder holder, int position) {
T item = getItem(position);
holder.bind(item);
holder.itemView.setOnClickListener(v -> {
if (onRecyclerItemClickListener != null) {
onRecyclerItemClickListener.onItemClick(this, item);
}
});
}
protected final View getViewHolderView(ViewGroup parent, @LayoutRes int itemLayoutId) {
return LayoutInflater.from(parent.getContext()).inflate(itemLayoutId, parent, false);
}
// ... other base methods such as getItemCount(), setItems, etc.
BaseViewHolder:
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
protected abstract void bind(T item);
public BaseViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public final Context getContext() {
return itemView.getContext();
}
用法:
final class MyAdapter extends BaseRecyclerAdapter<MyItem> {
MyAdapter(List<MyItem> items) { super(items); }
@Override public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(getViewHolderView(parent, R.layout.item));
}
class MyViewHolder extends BaseViewHolder<MyItem> {
@BindView(R.id.txt_date) TextView txtDate;
MyViewHolder(View itemView) { super(itemView); }
@Override protected void bind(MyItem item) {
txtDate.setText(getContext().getString(R.string.item_created, DateTimeUtils.formatDate(item.date)));
// put your null checking here
}
}