如何从这个ListView获取所选项目?

时间:2013-08-27 21:56:50

标签: android listview

我有一个自定义列表视图,如果用户点击某个项目,我一直试图进入下一个屏幕。它似乎不起作用。我试过,否则,切换。它可以工作,如果我不使用,但这样,每个项目将转到同一屏幕。任何帮助都会得到帮助。

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);
            }
        }
    });

}

3 个答案:

答案 0 :(得分:0)

您无法将字符串与&#34; ==&#34;进行比较。您需要做的是使用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);
    }
}

答案 1 :(得分: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

答案 2 :(得分: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;
       }

        }
    });

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