-6

我的代码检查if mode="search" true它何时进入第二个活动。如果是,那么School_name就是这个值,否则当用户点击ImageView或时设置这个值ListView

问题是,如果我单击表单 imageview 返回 true,则此语句if (( mode.compareTo("search")==0) )并从if (( mode.compareTo("search")==0) )if is 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");
        }
    }
}
4

2 回答 2

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");
}       
于 2013-09-09T12:14:03.077 回答
0

在列表事件处理程序中将模式设置为空。

mode = "";
于 2013-09-09T12:20:33.913 回答