@Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final OfflineRegion region = adapter.getItem(position);
    String metadata = OfflineUtils.convertRegionName(region.getMetadata());

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Rename metadata");

    final EditText input = new EditText(this);
    input.setText(metadata);
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    input.setSelection(metadata.length());
    builder.setView(input);

    builder.setPositiveButton(
        "OK",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            updateMetadata(region, OfflineUtils.convertRegionName(input.getText().toString()));
          }
        });
    builder.setNegativeButton(
        "Cancel",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
          }
        });

    builder.show();
  }
  private void updateMetadata(OfflineRegion region, byte[] metadata) {
    region.updateMetadata(
        metadata,
        new OfflineRegion.OfflineRegionUpdateMetadataCallback() {
          @Override
          public void onUpdate(byte[] metadata) {
            adapter.notifyDataSetChanged();
          }

          @Override
          public void onError(String error) {
            Toast.makeText(
                    UpdateMetadataActivity.this,
                    "Region metadata update failed with " + error,
                    Toast.LENGTH_LONG)
                .show();
          }
        });
  }