英文:
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<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;
}
// myViewHolder.tv_date.setText(""+new java.text.SimpleDateFormat("dd MMM").format(new java.util.Date (transacation.date)));
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;
}
}
答案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(""+new java.text.SimpleDateFormat("dd MMM").format(new java.util.Date (transacation.date)));
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;
}
答案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("+₹" + transacation.n_amount);
to
myViewHolder.tv_amount.setText("+₹" + String.ValueOf(transacation.n_amount));
or please post full Log value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论