简体   繁体   English

如何从此ListView中获取所选项目?

[英]How to get the selected item from this ListView?

I have a custom List view and I been trying to get to next screen if user clicks an item. 我有一个自定义的列表视图,如果用户单击某个项目,我一直试图进入下一个屏幕。 It doesn't seem to work. 它似乎不起作用。 I tried if else, switch. 我尝试过,如果其他,请切换。 It works if I don't use either but that way, every item will go to the same screen. 如果我不使用任何一个都可以,但是那样,每个项目都将进入同一屏幕。 Any help would be appericiated. 任何帮助将是适当的。

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

}

You can't compare Strings with "==". 您不能将字符串与“ ==”进行比较。 What you need to do is use the method String.equals(String). 您需要做的是使用String.equals(String)方法。 Something like: 就像是:

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

Try this 尝试这个

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

Try this, it will help you. 试试这个,它将对您有帮助。

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

        }
    });

You get over id the item you clicked. 您获得的ID超过了您单击的项目。 Then you can go to the right activity you want for this item. 然后,您可以转到该项目所需的正确活动。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM