【问题标题】:Text is not setting in BaseadapterBaseadapter 中未设置文本
【发布时间】:2015-12-31 07:16:47
【问题描述】:

我正在关注this 示例来实现EndlessScrolllistview,当我运行我的应用程序时应用程序崩溃并出现以下错误,任何帮助将不胜感激,谢谢

这是我的适配器类

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;
    }

错误

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)

第 109 行是

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

谁能帮忙??

【问题讨论】:

    标签: android json baseadapter


    【解决方案1】:

    您不需要将您的 holder 对象声明为 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();
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      你需要在if条件下使用setTag()

      if (convertView == null) {
              holder = new ViewHolder();
      
              convertView = LayoutInflater.from(ctx).inflate(R.layout.row_layout, null);
              holder.txtproname = (TextView) convertView.findViewById(R.id.txt1);
      
              convertView.setTag(holder);
      
          } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多