@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);
}
}
你可以给我一些关于如何重复使用而不是复制/粘贴的提示吗? 谢谢!
答案 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");