ListView自定义适配器在滚动时出现错误。

huangapple go评论74阅读模式
英文:

ListView Custom Adapter error on scrolling

问题

public class TransactionAdapter extends BaseAdapter {
    private static final String TAG = TransactionAdapter.class.getName();
    List<Transacation> arrlist;
    Context c;
    private LayoutInflater inflater;

    public TransactionAdapter(Context context, List<Transacation> list) {
        this.arrlist = list;
        this.c = context;
        inflater = LayoutInflater.from(this.c);
    }

    @Override
    public int getCount() {
        return arrlist.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View result;
        final MyViewHolder myViewHolder;
        myViewHolder = new MyViewHolder();
        final Transacation transacation = arrlist.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.transaction_adapter, parent,
                    false);

            myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
            myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
            myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
            myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);

            result = convertView;
            convertView.setTag(myViewHolder);

        } else {
            result = convertView;
        }

        if (transacation.transactionType.equals("CREDIT")) {
            myViewHolder.tv_status.setText("Wallet Credited");
            myViewHolder.tv_expiry.setText("Expires on " + new java.text.SimpleDateFormat("dd MMM yyyy").format(new java.util.Date(transacation.expiry_date)));
            myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);
            myViewHolder.tv_amount.setTextColor(Color.parseColor("#23DD47"));

        } else {
            myViewHolder.tv_status.setText("Wallet Expired");
            myViewHolder.tv_expiry.setVisibility(View.GONE);
            myViewHolder.tv_amount.setText("-₹" + transacation.n_amount);
            myViewHolder.tv_amount.setTextColor(Color.parseColor("#FB5151"));

        }

        return convertView;
    }

    private class MyViewHolder {
        TextView tv_date;
        TextView tv_status;
        TextView tv_expiry;
        TextView tv_amount;
    }
}
英文:

I have attached my code below of custom adapter of listView but when I am scrolling the list I got:

> mView does not exist, so discard the remaining points.
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.TextView.setText(java.lang.CharSequence)' on a null
> object reference
Can anybody help why I am getting this error it would be very helpfull

public class TransactionAdapter extends BaseAdapter {
private static final String TAG = TransactionAdapter.class.getName();
List&lt;Transacation&gt; arrlist;
Context c;
private LayoutInflater inflater;
public TransactionAdapter(Context context, List&lt;Transacation&gt; list) {
this.arrlist = list;
this.c = context;
inflater = LayoutInflater.from(this.c);
}
@Override
public int getCount() {
return arrlist.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View result;
final MyViewHolder myViewHolder;
myViewHolder = new MyViewHolder();
final Transacation transacation = arrlist.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_adapter, parent,
false);
myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);
result = convertView;
convertView.setTag(myViewHolder);
} else {
result = convertView;
}
//        myViewHolder.tv_date.setText(&quot;&quot;+new java.text.SimpleDateFormat(&quot;dd MMM&quot;).format(new java.util.Date (transacation.date)));
if(transacation.transactionType.equals(&quot;CREDIT&quot;)){
myViewHolder.tv_status.setText(&quot;Wallet Credited&quot;);
myViewHolder.tv_expiry.setText(&quot;Expires on &quot; + new java.text.SimpleDateFormat(&quot;dd MMM yyyy&quot;).format(new java.util.Date (transacation.expiry_date)));
myViewHolder.tv_amount.setText(&quot;+₹&quot; + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor(&quot;#23DD47&quot;));
}
else{
myViewHolder.tv_status.setText(&quot;Wallet Expired&quot;);
myViewHolder.tv_expiry.setVisibility(View.GONE);
myViewHolder.tv_amount.setText(&quot;-₹&quot; + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor(&quot;#FB5151&quot;));
}
return convertView;
}
private class MyViewHolder {
TextView tv_date;
TextView tv_status;
TextView tv_expiry;
TextView tv_amount;
}
}

答案1

得分: 1

你需要在convertView不为空时通过获取标签来构造ViewHolder,因此将你的getView()更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View result;
    final MyViewHolder myViewHolder;
    final Transacation transacation = arrlist.get(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.transaction_adapter, parent, false);
        myViewHolder = new MyViewHolder();
        myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
        myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
        myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
        myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);
        
        result = convertView;
        convertView.setTag(myViewHolder);
        
    } else {
        result = convertView;
        myViewHolder = (MyViewHolder) convertView.getTag();
    }

    if(transacation.transactionType.equals("CREDIT")){
        myViewHolder.tv_status.setText("Wallet Credited");
        myViewHolder.tv_expiry.setText("Expires on " + new java.text.SimpleDateFormat("dd MMM yyyy").format(new java.util.Date(transacation.expiry_date)));
        myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);
        myViewHolder.tv_amount.setTextColor(Color.parseColor("#23DD47"));
    
    }
    else{
        myViewHolder.tv_status.setText("Wallet Expired");
        myViewHolder.tv_expiry.setVisibility(View.GONE);
        myViewHolder.tv_amount.setText("-₹" + transacation.n_amount);
        myViewHolder.tv_amount.setTextColor(Color.parseColor("#FB5151"));
    
    }
    
    return convertView;
}
英文:

You need to construct the ViewHolder when the convertView is not null by getting the tag back, so change your getView() to be:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View result;
final MyViewHolder myViewHolder;
final Transacation transacation = arrlist.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_adapter, parent,
false);
myViewHolder = new MyViewHolder()
myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);
result = convertView;
convertView.setTag(myViewHolder);
} else {
result = convertView;
myViewHolder = (MyViewHolder) convertView.getTag();
}
//        myViewHolder.tv_date.setText(&quot;&quot;+new java.text.SimpleDateFormat(&quot;dd MMM&quot;).format(new java.util.Date (transacation.date)));
if(transacation.transactionType.equals(&quot;CREDIT&quot;)){
myViewHolder.tv_status.setText(&quot;Wallet Credited&quot;);
myViewHolder.tv_expiry.setText(&quot;Expires on &quot; + new java.text.SimpleDateFormat(&quot;dd MMM yyyy&quot;).format(new java.util.Date (transacation.expiry_date)));
myViewHolder.tv_amount.setText(&quot;+₹&quot; + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor(&quot;#23DD47&quot;));
}
else{
myViewHolder.tv_status.setText(&quot;Wallet Expired&quot;);
myViewHolder.tv_expiry.setVisibility(View.GONE);
myViewHolder.tv_amount.setText(&quot;-₹&quot; + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor(&quot;#FB5151&quot;));
}
return convertView;
}

答案2

得分: 0

依我看,请检查一下 findViewById 是否有正确的 Id 名称,或者如果与此无关,请从以下位置进行更新:

从:

myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);

改为:

myViewHolder.tv_amount.setText("+₹" + String.ValueOf(transacation.n_amount));

或者请提供完整的日志数值。

英文:

in my opinion please check if the findViewById has the correct Id name or if nothing to do with it please update from

myViewHolder.tv_amount.setText(&quot;+₹&quot; + transacation.n_amount);

to

myViewHolder.tv_amount.setText(&quot;+₹&quot; + String.ValueOf(transacation.n_amount));

or please post full Log value

huangapple
  • 本文由 发表于 2020年4月10日 13:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定