user1735856
user1735856

Reputation: 285

Avoid Code Duplicate Android Class

I have the same code in 2 Classes of my android-project. I´m just begin to learn java / android, so pls could you give me some tips? Do I have set a new class? Thank you for your help!

Main.java

public class Main extends Activity {


private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnGallery = (Button) findViewById(R.id.btnGallery);
    btnGallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);

    Intent uploadActivity = new Intent(Main.this, Upload.class);
    uploadActivity.putExtra("picturePath", picturePath);
    startActivity(uploadActivity);
    cursor.close();
}


 }
 }

And Upload.java

  public class Upload extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);
    Bundle extras = getIntent().getExtras();
    String picturePathView = extras.getString("picturePath");
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    findViewById(R.id.imgView).setScrollBarStyle(
            View.SCROLLBARS_INSIDE_INSET);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));

    Button btnGallery = (Button) findViewById(R.id.btnGallery);
    btnGallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        findViewById(R.id.imgView).setScrollBarStyle(
                View.SCROLLBARS_INSIDE_INSET);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        cursor.close();
    }

}
  }

Upvotes: 0

Views: 739

Answers (3)

Arslan Ali
Arslan Ali

Reputation: 17802

Sure you can put all the code in one class. I'm not gonna write all the code but you will get the idea how to gain refactoring in your code.

For onClickListner, you can do the following:

@Override
public void onClick(View clickedView) {

    int clickedViewId = clickedView.getId();

    switch(clickedViewId) {
        case R.id.firstItem:
            // code when first view is clicked
        case R.id.secondItem:
            // code when second view is clicked
        default:
            // optional
    }
}

And then setting the same onClickListner for both views.

The same kind of switch statement you can use in onActivityResult method. I hope now you can merge the two classes in one.

Upvotes: 0

fish
fish

Reputation: 828

This is actually what hasanghaforian said, but a little bit more conrect.

Your abstract Activity which enables holds the properties which are the same for Upload and Main could be the following

public abstract class AbstractMediaPickerActivity extends Activity {

    protected static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void startMediaPicker() {
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    protected abstract void onImagePicked(String picturePath);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);

            cursor.close();

            onImagePicked(picturePath);

        }

    }
}

Your both other classes are inherited by the AbstractMediaPickerActivity

public class MainActivity extends AbstractMediaPickerActivity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnGallery = (Button) findViewById(R.id.btnGallery);
        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startMediaPicker();
            }
        });
    }

    protected void onImagePicked(String picturePath) {
        Intent uploadActivity = new Intent(Main.this, Upload.class);
        uploadActivity.putExtra("picturePath", picturePath);
        startActivity(uploadActivity);
    }

}


public class Upload extends AbstractMediaPickerActivity {


    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        Bundle extras = getIntent().getExtras();
        String picturePathView = extras.getString("picturePath");
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        findViewById(R.id.imgView).setScrollBarStyle(
                View.SCROLLBARS_INSIDE_INSET);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePathView));

        Button btnGallery = (Button) findViewById(R.id.btnGallery);
        btnGallery.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                startMediaPicker();
            }
        });
    }

    @Override
    protected void onImagePicked(String picturePath) {
           ImageView imageView = (ImageView) findViewById(R.id.imgView);
            findViewById(R.id.imgView).setScrollBarStyle(
                    View.SCROLLBARS_INSIDE_INSET);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));    
    }

}

Upvotes: 1

hasanghaforian
hasanghaforian

Reputation: 14022

Create a Class(for example SuperActivity) that extends Activity and add duplicated code to it.Then create Activities A,B that extend SuperActivity.

Upvotes: 0

Related Questions