0

我有一个自定义列表视图,如果用户单击一个项目,我会尝试进入下一个屏幕。它似乎不起作用。我试过如果其他,切换。如果我不使用它们,它会起作用,但这样的话,每个项目都会进入同一个屏幕。任何帮助都会得到帮助。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options_main);

    OptionMainHeader option_list[] = new OptionMainHeader[]
    {
        new OptionMainHeader(R.drawable.clock_icon_48, "Helo"),
        new OptionMainHeader(R.drawable.weather_cloudy, "Apple"),
        new OptionMainHeader(R.drawable.weather_cloudy, "Mango"),
        new OptionMainHeader(R.drawable.weather_cloudy, "Banana"),

        };

    OptionMainHeader1 adapter = new OptionMainHeader1(this, R.layout.option_main_header1, option_list);

    listView1 = (ListView)findViewById(R.id.listView1);

    View header = (View)getLayoutInflater().inflate(R.layout.option_main_header, null);
    listView1.addHeaderView(header);
    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int position = listView1.getSelectedItemPosition();
            String selectedFromList =(String) (listView1.getItemAtPosition(position));

            if (selectedFromList == "Helo"){
                Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
                startActivity(CurrentWaitTime);
            } else if (selectedFromList == "Apple"){
                Intent Appointments = new Intent(getApplicationContext(), no.class);
                startActivity(Appointments);
            } else if (selectedFromList == "Mango"){
                Intent Logout = new Intent(getApplicationContext(), go.class);
                startActivity(Logout);
            } else if (selectedFromList == "Banana"){
                Intent Exit = new Intent(getApplicationContext(), low.class);
                startActivity(Exit);
            }
        }
    });

}
4

3 回答 3

0

您不能将字符串与“==”进行比较。您需要做的是使用方法 String.equals(String)。就像是:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String selectedFromList = option_list[position].getTHATSTRING();
    if (selectedFromList.equals("Helo")){
        Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
        startActivity(CurrentWaitTime);
    } else if (selectedFromLis.equals("Apple")){
        Intent Appointments = new Intent(getApplicationContext(), no.class);
        startActivity(Appointments);
    } else if (selectedFromList.equals("Mango")){
        Intent Logout = new Intent(getApplicationContext(), go.class);
        startActivity(Logout);
    } else if (selectedFromList.equals("Banana")){
        Intent Exit = new Intent(getApplicationContext(), low.class);
        startActivity(Exit);
    }
}
于 2013-08-27T22:01:57.377 回答
0

试试这个,它会帮助你。

   listView1.setOnItemClickListener(new OnItemClickListener() {

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

        //global variable
        idItem = (int) id;

        switch (idItem) {

       case 0:
      // This is your first Item

      // Add your Intent and startActivity
   break;

       case 1:
   // Add your Intent and startActivity
   break;

       case 2:
   // Add your Intent and startActivity

   break;

       case 3:
   // Add your Intent and startActivity

   break;
       }

        }
    });

你得到了你点击的项目的 id 。然后,您可以针对此项目进行所需的正确活动。希望能帮助到你。

于 2013-08-28T06:44:11.483 回答
0

尝试这个

ListView1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
 TextView textView = (TextView) arg1;
String strText = textView.getText().toString();
       if ("Helo".equals(strText)){
           Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
           startActivity(CurrentWaitTime);
    } else if ("Apple".equals(strText){
            Intent Appointments = new Intent(getApplicationContext(), no.class);
            startActivity(Appointments);
        } etc, etc
于 2013-08-27T22:04:50.897 回答