【问题标题】:How to load an specific activity(Intent) on Listview(item)Click?如何在 Listview(item)Click 上加载特定活动(Intent)?
【发布时间】:2013-09-23 10:55:24
【问题描述】:
 package com.example.app;

 import com.example.app.adaptor.*;
 import android.app.Activity;
 import android.app.ListActivity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.ListView;


 public class ListviewExample extends ListActivity {

static final String[] ASDFGH = new String[] { "ABC", "PQR",
        "XYZ", "RAA","AA" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setListAdapter(new etcArray(this, main_activity ));

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    // get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
            if(selectedValue.equalsIgnoreCase("ABC"))
    {
        Intent in = new Intent(getApplicationContext(),ABC.class);
        startActivity(in);
    }
    else
        if(selectedValue.equalsIgnoreCase("AA"))
        {
            Intent in = new Intent(getApplicationContext(),AA.class);         
            startActivity(in);
        }

   }

在上面的程序中..使用像 if string.equalsIgnorecase("AA") 这样的正常条件 我们可以在该特定点击上启动意图!是否有任何方法可以在任何列表视图项目单击中启动活动???

【问题讨论】:

    标签: android listview android-intent


    【解决方案1】:

    还可以创建类数组,这样您就可以轻松地指向确切的活动...

    static final Class[] classArray = new Class[] { ABC.class,PQR.class,XYZ.class,RAA.class,AA.class };
    static final String[] ASDFGH = new String[] { "ABC", "PQR",
    "XYZ", "RAA","AA" };
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    Intent intent=newIntent(getApplicationContext(),classArray[position]);
    startActivity(intent);
    }
    

    【讨论】:

    • 感谢您的帮助..类型不匹配错误:您无法将 String[] 转换为 Class[]
    • 请立即检查更新.. 实际上它是 Class 对象。错误地我写了字符串..
    【解决方案2】:

    尝试运行以下代码。

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    
    
        // get selected items
    
        if(position == 0)
        {
            Intent intent=newIntent(this, Class1.class);
            startActivity(intent);
        } else if(poition == 1)
        {
             Intent intent=newIntent(this, Class2.class);
             startActivity(intent);
        } else if(poition == 1)
            Intent intent=newIntent(this, Class3.class);
             startActivity(intent);
    
    }  
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码:

      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
          Class<?> nextClass = null;
          // get selected items
          String selectedValue = (String) getListAdapter().getItem(position);
          if(selectedValue.equalsIgnoreCase("ABC"))
              nextClass = ABC.class;
          else if(selectedValue.equalsIgnoreCase("AA"))
              nextClass = AA.class;
          if(nextClass != null)
              startActivity(new Intent(context, nextClass));
      
         }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多