我的代码在进入第二个活动时检查if mode="search" true
。如果是,则School_name
为此值,否则在用户点击ImageView
或ListView
时设置该值。
问题是,如果我点击表单imageview返回true,则此语句if (( mode.compareTo("search")==0) )
并从if (( mode.compareTo("search")==0) )
获取值如果是flase not go else语句告诉我代码中有什么问题
public class HomeMenu extends Activity {
public static String mode;
editText2=(EditText) findViewById(R.id.editText2);
ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick (View v){
mode = "search";
Intent iMenuList = new Intent(HomeMenu.this, SecondStep.class);
Serachvalue = editText2.getText().toString();
iMenuList.putExtra("Serachvalue", Serachvalue);
startActivity(iMenuList);
}
});
listMainMenuhome=(ListView) findViewById(R.id.listMainMenuhome);
listMainMenuhome.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView < ? > arg0, View arg1,int position, long arg3){
Intent iMenuList = new Intent(HomeMenu.this, SecondStep.class);
iMenuList.putExtra("School_name", Category_name.get(position));
startActivity(iMenuList);
}
});
public class SecondStep extends Activity {
String mode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondstep);
mode = HomeMenu.mode;
if ((mode.compareTo("search") == 0)) {
Bundle bundle = this.getIntent().getExtras();
School_name = bundle.getString("Serachvalue");
} else {
Bundle bundle = this.getIntent().getExtras();
School_name = bundle.getString("School_name");
}
}
}
答案 0 :(得分:2)
listMainMenuhome.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
mode="";
Intent iMenuList = new Intent(HomeMenu.this, SecondStep.class);
iMenuList.putExtra("School_name", Category_name.get(position));
startActivity(iMenuList);
}
});
mode=HomeMenu.mode;
if (mode.equalsIgnoreCase("search"))
{
Bundle bundle = this.getIntent().getExtras();
School_name = bundle.getString("Serachvalue");
}
else
{
Bundle bundle = this.getIntent().getExtras();
School_name = bundle.getString("School_name");
}
答案 1 :(得分:0)
在列表事件处理程序中将模式设置为空。
mode = "";