简体   繁体   中英

access non final variable from anonymous inner class

public class FoodTypeAdapter extends BaseAdapter {
private Context context;

public FoodTypeAdapter(Context context) {
    this.context = context;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.food_type_grid,
                new LinearLayout(context));
        holder = new ViewHolder();          
        holder.btnAdd = (Button) convertView.findViewById(R.id.btnItemAdd);
        holder.etQty = (EditText) convertView.findViewById(R.id.etfItemQty);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.btnAdd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String quantity = holder.etQty.getText().toString();
            System.out.println(quantity);
        }
    });
    return convertView;
}

public int getCount() {
    return mylist.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

static class ViewHolder {       
    Button btnAdd;
    EditText etQty;
}
}

In this code without make holder as final Object I can't access it from OnClickListener .

If I make it as final Object I can't initiate holder like holder = new ViewHolder(); .

Now what can I do? Any help will be highly appreciable.

Thanks, Guna

You also could have simply adjusted the initialization of holder which reduced your code complexity by the way:

final ViewHolder holder;

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.food_type_grid,
            new LinearLayout(context));
    holder = new ViewHolder();
    holder.btnAdd = (Button) convertView.findViewById(R.id.btnItemAdd);
    holder.etQty = (EditText) convertView.findViewById(R.id.etfItemQty);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

You can work around it by adding a temporary final variable before the anonymous class, and use that variable instead:

final ViewHolder finalHolder = holder;    // <- added

holder.btnAdd.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        String quantity = finalHolder.etQty.getText().toString();
        //                ^^^^^^^^^^^
        System.out.println(quantity);
    }
});

Or, you could make holder a member variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM