【问题标题】:How to use intent when using custom listview使用自定义列表视图时如何使用意图
【发布时间】:2015-12-03 15:56:54
【问题描述】:

如何使用与自定义listview 不同的意图活动。

代码

@SuppressLint("NewApi")
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);

    ActionBar actionBar = getActionBar();

    // set the icon
    actionBar.setIcon(R.drawable.excel);
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#546E7A")));

    ArrayList<Excel> list = new ArrayList<Excel>();
    list.add(new Excel("Google", "Android"));
    list.add(new Excel("Apple", "IOS"));

    ListAdapter adapter = new ListAdapter(this, list);

    ListView listView = (ListView) findViewById(R.id.id_list);
    listView.setAdapter(adapter);

    // event handler click item of list
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
            public void onItemClick(AdapterView arg0, View arg1,
            int position, long arg3) {
                    // call new layout with intent
                Intent intent = new Intent(MainActivity.this, Apple.class);
            startActivity(intent);

        }
    });
}
}

当我点击所有项目时,代码只是查看 Apple.class 活动,如何运行不同的活动示例:

Item Google run Google.class

Item Apple 运行 Apple.class

【问题讨论】:

  • 使用 if else 并开始适当的活动。

标签: android android-intent android-listview


【解决方案1】:

根据您当前的代码使用

Intent intent;
if(position==0){
     intent = new Intent(MainActivity.this, Google.class);
}else if(position==1){
   intent = new Intent(MainActivity.this, Apple.class);
}
startActivity(intent);

【讨论】:

    【解决方案2】:
        public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        switch( position )
        {
          case 0:  Intent newActivity = new Intent(this, Apple.class);     
                 startActivity(newActivity);
                 break;
          case 1:  Intent newActivity = new Intent(this, Google.class);     
                  startActivity(newActivity);
                 break;
    
    
        }
     }
    

    【讨论】:

    • 非常感谢。 ?
    【解决方案3】:

    使用反射获取Class。类名必须包含packagename。这样。

    try{
                   Class classType=Class.forName("com.zfeng.stackquesimg.Google");
                    Intent intent=new Intent();
                    intent.setClass(MainActivity.this,classType);
                    startActivity(intent);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }
    

    【讨论】:

      【解决方案4】:

      这样试试

         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      
              @Override
                  public void onItemClick(AdapterView arg0, View arg1,
                  int position, long arg3) {
                  switch(Position)
                      {
                        case 0:
                         //Call Google Intent
                       break;
                       case 1:
                          // call new layout with intent
                      Intent intent = new Intent(MainActivity.this, Apple.class);
                       startActivity(intent);
                        break;
                      default:
                    // default code
                     break;
                    }
      
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多