避免代码重复Android类

时间:2013-03-23 16:47:02

标签: java android performance

我在android-project的2个类中有相同的代码。我刚刚开始学习java / android,所以请你给我一些提示吗?我是否设置了新课程?谢谢你的帮助!

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();
}


 }
 }

和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();
    }

}
  }

3 个答案:

答案 0 :(得分:1)

这实际上是hasanghaforian所说的,但更为正确。

您启用的抽象活动包含与上传和主要相同的属性,可以是以下

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);

        }

    }
}

您的其他两个类都由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));    
    }

}

答案 1 :(得分:0)

创建一个类(例如SuperActivity),扩展Activity并向其添加重复的代码。然后创建扩展SuperActivity的活动A,B。

答案 2 :(得分:0)

当然,您可以将所有代码放在一个类中。我不会编写所有代码,但您会了解如何在代码中获得重构。

对于onClickListner,您可以执行以下操作:

@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
    }
}

然后为两个视图设置相同的onClickListner

您可以在onActivityResult方法中使用相同类型的switch语句。我希望你现在可以合并这两个类。