0

I have a adapter with array_restaruants is json when 1 object but method get view run 7 times dont 1 time is correct

if in method get count force return 1 dont work i dont have idea for problem is my base adapter or why?

all time enter 7 times

public class AdapterName extends BaseAdapter {
LayoutInflater inflater;
Activity activity;
private JSONArray array_restaruants;
private AQuery aq;


public AdapterName(Activity a,JSONArray cur_array_restaruants){
    activity= a;
    array_restaruants=cur_array_restaruants;

    aq=new AQuery(activity);
    inflater =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



public static class ViewHolder{

    private Responsive_Linear_Layout linear_content;
    private ImageView logo_empresa;


}

@Override
public int getCount() {
    return array_restaruants.length();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;

    if(convertView == null){

        vi = inflater.inflate(R.layout.template_parent_view, parent,false);


        holder = new ViewHolder();
        holder.linear_content = (Responsive_Linear_Layout) vi.findViewById(R.id.linear_content);
        holder.logo_empresa = (ImageView) vi.findViewById(R.id.logo_empresa);



        vi.setTag( holder );
    }
    else {
        holder = (ViewHolder) vi.getTag();

        try {
            JSONObject obj_restaurant = array_restaruants.getJSONObject(position);
            JSONArray coupons = obj_restaurant.getJSONArray("coupons");
            Log.e("obj_restaurant", obj_restaurant.toString());
            aq.id(holder.logo_empresa).image(obj_restaurant.getString("logo"), false, false);

            for (int i = 0; i < coupons.length(); i++) {
                LayoutInflater inflater2 = LayoutInflater.from(activity);

                JSONObject obj_coupons = coupons.getJSONObject(i);
                View vi2 = inflater2.inflate(R.layout.template_for_items, null, false);
                vi2.setId(i);
                final TextView text_coupon = (TextView) vi2.findViewById(R.id.text_coupon);
                text_coupon.setText(obj_coupons.getString("name"));
                text_coupon.setId(obj_coupons.getInt("id"));

                text_coupon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Toast.makeText(activity, String.valueOf(v.getId()), Toast.LENGTH_LONG).show();


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });        imageButton.setBackground(activity.getResources().getDrawable(R.drawable.login_usuario_1));
                holder.linear_content.addView(vi2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return vi;
}

}

pedroooo
  • 563
  • 1
  • 4
  • 17
  • 1
    Possible duplicate of [custom listview adapter getView method being called multiple times, and in no coherent order](http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co) – Mike M. Apr 26 '16 at 05:01

1 Answers1

-2

Probably you set height of your ListView to wrap_content, you should not do that. Make sure your height is match_parent. Watch google io video about ListView: https://www.youtube.com/watch?v=wDBM6wVEO70 It has some tips.

Murat Mustafin
  • 1,284
  • 10
  • 17