Use try/catch to declare and initialize final variables
I’m trying to declare and initialize the final variable in the adapter to use that variable declaration in OnLongClickListener().
The problem is that the variable is being initialized by a method that may throw a failure exception, so initialization must occur in the context of try/catch. In the exception block, there is a fallback value to which the final variable is assigned. This all happens with the ArrayAdapter<T>
in the rewritten getView(
).
My question is, is there a clever way to do this? I solved this problem by declaring a temporary variable outside of try/catch and initializing it in try/catch, and then declaring and initializing the final variable with the result of the temporary variable after try/catch block. It feels so messy.
Code:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) Data.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
begin eww gross
Vehicle temp;
try {
temp = Data.getVehicle(values.get(position).getId());
} catch (ObjectNotFoundException e) {
Log.e(TAG, e.getMessage() + "-- unable to find vehicle while getting list item vehicle for vehicles tab fragment");
temp = values.get(position);
}
final Vehicle vehicle = temp;
end disappointing, ugly workaround
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.vehicle_item_with_img, null);
holder = new ViewHolder();
holder.vehicleLabel = (TextView) convertView.findViewById(R.id.vehicle_label);
holder.statusImage = (ImageView) convertView.findViewById(R.id.lmImageView);
holder.sImage = (ImageView) convertView.findViewById(R.id.sImageView);
holder.vehicleTag = vehicle.getId();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (Data.isFollowingVehicle(vehicle)) {
holder.sImage.setVisibility(View.VISIBLE);
}
else {
holder.sImage.setVisibility(View.INVISIBLE);
}
holder.statusImage.setImageDrawable(vehicle.getListIcon());
holder.vehicleLabel.setText(vehicle.getFormattedLabel());
final ViewHolder finalHolder = holder;
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!vehicle.isFollowed()) {
finalHolder.sImage.setVisibility(View.VISIBLE);
mapInterface.follow(vehicle);
} else {
finalHolder.sImage.setVisibility(View.GONE);
mapInterface.unfollow(vehicle);
}
return false;
}
});
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mapInterface.handleVehicleClickFromList(vehicle);
}
});
return convertView;
}
Solution
You can move the logic for getting a Vehicle into its own method. It makes it reusable and easier to read.
private Vehicle getVehicle(int position) {
try {
return Data.getVehicle(values.get(position).getId());
} catch (ObjectNotFoundException e) {
Log.e(TAG, e.getMessage() + "-- unable to find vehicle while getting list item vehicle for vehicles tab fragment");
return values.get(position);
}
}
Then go back to the original method:
final Vehicle vehicle = getVehicle(position);