尽力不重复自己(android / java)

时间:2014-07-08 11:58:25

标签: java android class methods

怎么了? 我是一个发展的初学者,在我读到的任何地方都不要重复自己,所以我真的很想重复自己。 有一段代码我复制并粘贴在我的应用程序的每一个活动中,我怀疑可能有一种方法不重复它 - 也许创建一个类并调用它的方法 - 但我不知道如何实现它。 下面的代码就是我所说的:我在导航抽屉中使用的东西,它在我的应用程序的每个活动的主要类中:

@Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position,
                long arg3) {

            if (position == 0) {
                Toast.makeText(this, "categories baby", Toast.LENGTH_SHORT).show();
            } else if (position == 1){

                final Context context = this;

                Intent getUserData = getIntent();
                String userEmail = getUserData.getStringExtra("user_email");

                Intent intent = new Intent(context, NewAdActivity.class);
                intent.putExtra("userEmail", userEmail);
                startActivity(intent);


            } else if (position == 2){

                final Context context = this;

                Intent getUserData = getIntent();
                String userEmail = getUserData.getStringExtra("user_email");

                Intent intent = new Intent(context, AdListActivity.class);
                intent.putExtra("userEmail", userEmail);
                startActivity(intent);

            } else {
                Session session = Session.getActiveSession();
                session.closeAndClearTokenInformation();

                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
            }
        }

你可以给我一些关于如何重复使用而不是复制/粘贴的提示吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

您可以创建一个扩展Activity的新类并实现您的代码,然后让您的所有活动扩展该类。

该代码似乎是AdapterView.OnItemClickListener的实现,因此您可以创建类似:

的内容
public abstract class MyActivityWithListener extends Activity 
        implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
        ....
    }

}

然后你的主要活动可能是:

public class MainActivity extends MyActivityWithListener {
    ...
}

答案 1 :(得分:1)

您可以在以下块中清楚地看到

        } else if (position == 1){

            final Context context = this;

            Intent getUserData = getIntent();
            String userEmail = getUserData.getStringExtra("user_email");

            Intent intent = new Intent(context, NewAdActivity.class);
            intent.putExtra("userEmail", userEmail);
            startActivity(intent);


        } else if (position == 2){

            final Context context = this;

            Intent getUserData = getIntent();
            String userEmail = getUserData.getStringExtra("user_email");

            Intent intent = new Intent(context, AdListActivity.class);
            intent.putExtra("userEmail", userEmail);
            startActivity(intent);

        }

除了活动的类之外,您正在执行两次相同的操作。因此,你可以把它撕成一个像这样的函数,

        } else if (position == 1){
            beginAdActivity(NewAdActivity.class);
        } else if (position == 2){
            beginAdActivity(AdListActivity.class);
        }
 }

 public void beginAdActivity(Class<?> activityClass)
 {
      final Context context = this;

      Intent getUserData = getIntent();
      String userEmail = getUserData.getStringExtra("user_email");

      Intent intent = new Intent(context, activityClass);
      intent.putExtra("userEmail", userEmail);
      startActivity(intent);
 }

ta-dah,它只写了一次。

答案 2 :(得分:0)

只需像这样创建一个util类

public class MyUtils{

    public static void jumpToNewAd(Activity mContext){
        Intent getUserData = mContext.getIntent();
        String userEmail = getUserData.getStringExtra("user_email");

        Intent intent = new Intent(mContext, NewAdActivity.class);
        intent.putExtra("userEmail", userEmail);
        mContext.startActivity(intent);
    }
    public static void jumpToAdList(Activity mContext){
        Intent getUserData = mContext.getIntent();
        String userEmail = getUserData.getStringExtra("user_email");

        Intent intent = new Intent(mContext, AdListActivity.class);
        intent.putExtra("userEmail", userEmail);
        mContext.startActivity(intent);
    }
    public static void jumpToMain(Activity mContext){
        Session session = Session.getActiveSession();
        session.closeAndClearTokenInformation();

        Intent intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);
    }

    public static void ting(Activity mContext,String message){
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }

}

在任意位置调用

MyUtils.jumpToAdList(this);
        MyUtils.jumpToNewAd(this);
        MyUtils.jumpToMain(this);
        MyUtils.ting(this,"Catagories Baby");