从BaseAdapter的getView获取方法中的值

时间:2018-07-04 04:24:21

标签: java android onclick baseadapter custom-adapter

这是我的自定义适配器。我在这里有三个ImageViews。我想为每个imageview提供onclick。

首先,我在单击ImageView时调用了一个方法。在那种方法中,我需要替换为另一个图像。并且还需要替换其余两个Imageview图像。

现在,当我单击第一个imageview时,方法正在调用并且图像也为此更改。但是对于剩下的两个Imageview,它显示了NULL POINTER EXCEPTION。

如何使用该方法访问那些ImageView。

任何帮助将不胜感激。

public class CustomTestingProductsListAdapter extends BaseAdapter{
public CustomTestingProductsListAdapter(Context context, Activity activity, List<V_APP_PACK_QR_DET_INFO> pack_qr_det_infos) {
    layoutInFlater = LayoutInflater.from(context);
    this.pack_qr_det_infos = pack_qr_det_infos;
    this.curActivity = activity;
    this.mContext = (VilanApplication) VilanApplication.mContext;
}

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

@Override
public Object getItem(int position) {
    return pack_qr_det_infos.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try{
        CustomTestingProductsListAdapter.ViewHolder holder;
        if(convertView == null){
            holder = new CustomTestingProductsListAdapter.ViewHolder();
            convertView = layoutInFlater.inflate(R.layout.tst_ver_prdouct_list_items,null);
            holder.tvProduct = convertView.findViewById(R.id.tv_qc_productName);
            holder.DLSno = convertView.findViewById(R.id.tvtst_DLSno);
            holder.iv_tst_received = convertView.findViewById(R.id.iv_tst_received);
            holder.iv_tst_not_received = convertView.findViewById(R.id.iv_tst_not_received);
            holder.iv_tst_wrong_product = convertView.findViewById(R.id.iv_tst_wrong_product);
            convertView.setTag(holder);
        }else{
            holder = (CustomTestingProductsListAdapter.ViewHolder) convertView.getTag();
        }
        holder.DLSno.setText(pack_qr_det_infos.get(position).PRODUCTSLNO);
        holder.iv_tst_received.setOnClickListener(onClickListener(position));
    }catch (Exception e){
        Log.d(VilanConstants.TAG,"/Excp @listAdp"+ e.toString());
    }
    return convertView;
}

private View.OnClickListener onClickListener(final int position){
    return new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                CustomTestingProductsListAdapter.ViewHolder holder;
                holder = new CustomTestingProductsListAdapter.ViewHolder();
                holder.iv_tst_received = v.findViewById(R.id.iv_tst_received);
                holder.iv_tst_not_received = v.findViewById(R.id.iv_tst_not_received);
                holder.iv_tst_wrong_product = v.findViewById(R.id.iv_tst_wrong_product);
                Toast.makeText(mContext, "Item Received" , Toast.LENGTH_SHORT).show();
                holder.iv_tst_received.setImageResource(R.drawable.ic_received_green);
                holder.iv_tst_not_received.setImageResource(R.drawable.ic_not_received);
                holder.iv_tst_wrong_product.setImageResource(R.drawable.ic_wrong_product);
                Status = 0 ;
            }catch (Exception e){
                Log.e(VilanConstants.TAG,"/Excp due to"+e.toString());
            }

        }
    };
}


public class ViewHolder{
    private TextView tvProduct;
    private TextView DLSno;
    private ImageView iv_tst_received;
    private ImageView iv_tst_not_received;
    private ImageView iv_tst_wrong_product;
}
}

1 个答案:

答案 0 :(得分:0)

Update Your getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try{
        CustomTestingProductsListAdapter.ViewHolder holder;
        if(convertView == null){
            holder = new CustomTestingProductsListAdapter.ViewHolder();
            convertView = layoutInFlater.inflate(R.layout.tst_ver_prdouct_list_items,null);
            holder.tvProduct = convertView.findViewById(R.id.tv_qc_productName);
            holder.DLSno = convertView.findViewById(R.id.tvtst_DLSno);
            holder.iv_tst_received = convertView.findViewById(R.id.iv_tst_received);
            holder.iv_tst_not_received = convertView.findViewById(R.id.iv_tst_not_received);
            holder.iv_tst_wrong_product = convertView.findViewById(R.id.iv_tst_wrong_product);
            convertView.setTag(holder);
        }else{
            holder = (CustomTestingProductsListAdapter.ViewHolder) convertView.getTag();
        }
        holder.DLSno.setText(pack_qr_det_infos.get(position).PRODUCTSLNO);
        holder.iv_tst_received.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "Item Received" , Toast.LENGTH_SHORT).show();
                holder.iv_tst_received.setImageResource(R.drawable.ic_received_green);
                holder.iv_tst_not_received.setImageResource(R.drawable.ic_not_received);
                holder.iv_tst_wrong_product.setImageResource(R.drawable.ic_wrong_product);
                Status = 0 ;
            }
        });
    }catch (Exception e){
        Log.d(VilanConstants.TAG,"/Excp @listAdp"+ e.toString());
    }
    return convertView;
}