Search code examples
androidjsonbaseadapter

Text is not setting in Baseadapter


I am following this example to implement EndlessScrolllistview, when i run my app app got crash and gives following error,any help would be appreciate,Thanks

This is my adapter class

public class EndlessAdapter extends BaseAdapter {

    //private List<String> itemList;
    private Context ctx;
    private int layoutId;


    private ArrayList<HashMap<String, String>> listData;
    //private AQuery aQuery;
    String rup = "\u20B9";
    //private ArrayList<String> usersizesArrayList;

    private static final String TAG_NAME = "name";
    private static final String TAG_IMAGE = "img_url";
    private static final String TAG_PRICE = "price";
    private static final String TAG_ID_PRODUCT = "id_product";
    /*public EndlessAdapter(Context ctx, List<String> itemList, int layoutId) {
        super(ctx, layoutId, itemList);
        this.itemList = itemList;
        this.ctx = ctx;
        this.layoutId = layoutId;
    }*/

    public EndlessAdapter(Context ctx, ArrayList<HashMap<String, String>> listData) {
        this.ctx = ctx;
        this.listData = listData;
        //aQuery = new AQuery(this.context);
    }

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

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

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

    /*@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View result = convertView;

        if (result == null) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            result = inflater.inflate(layoutId, parent, false);
        }

        // We should use class holder pattern
        TextView tv = (TextView) result.findViewById(R.id.txt1);
        //tv.setText(itemList.get(position));
        */

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
         final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(ctx).inflate(R.layout.row_layout, null);
            holder.txtproname = (TextView) convertView.findViewById(R.id.txt1);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtproname.setText(listData.get(position).get(TAG_NAME));
        return convertView;

    }
    class ViewHolder {
        public EditText qunatityss;
        public TextView bordrsfourth;
        public TextView bordrsthird;
        public TextView bordrssecond;
        public TextView bordrs;
        public RadioButton productsizesmall;
        public RadioButton productsizemed;
        public RadioButton productsizelarge;
        public RadioButton productsizexlarge;
        public TextView fourthcolor;
        public TextView thirdcolor;
        public TextView secondcolor;
        public ImageView cartview;
        TextView txtprice;
        ImageView propic;
        TextView txtproname;
        TextView firstcolor;
        Button addtocart;
    }

Error

12-31 12:43:52.611: E/AndroidRuntime(6029): FATAL EXCEPTION: main
12-31 12:43:52.611: E/AndroidRuntime(6029): java.lang.NullPointerException
12-31 12:43:52.611: E/AndroidRuntime(6029):     at com.survivingwithandroid.endlessadapter.EndlessAdapter.getView(EndlessAdapter.java:109)

line number 109 is

holder.txtproname.setText(listData.get(position).get(TAG_NAME));

Can anyone help??


Solution

  • You don't need to declare your holder object as final.

    public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolder holder;
    
        if (convertView == null) {
    
            convertView = LayoutInflater.from(ctx).inflate(R.layout.row_layout, null);
    
            holder = new ViewHolder();
    
            holder.txtproname = (TextView) convertView.findViewById(R.id.txt1);
    
            convertView.setTag(holder);
    
        } 
    
        else {
    
            holder = (ViewHolder) convertView.getTag();
    
        }
    
    }