简体   繁体   English

如何在Listview(item)Click上加载特定活动(Intent)?

[英]How to load an specific activity(Intent) on Listview(item)Click?

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

   }

In the above program ..using normal condition like if string.equalsIgnorecase("AA") we can launch the intent on that specific click !! 在上面的程序中,使用正常条件(如string.equalsIgnorecase(“ AA”)),我们可以在该特定单击上启动意图! is there any method to launch an activity in any listview Item Click ??? 有什么方法可以在任何列表视图中启动活动吗?单击???

Create class array as well So you can point to exact activity easily ... 也创建类数组,因此您可以轻松地指向确切的活动...

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

You can use following code : 您可以使用以下代码:

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

   }  

Try to run following code. 尝试运行以下代码。

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

}  

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

相关问题 如何在筛选器ListView- Item-Click上加载特定活动(多个意图)? - How to load an specific activity (multiple intent) on filter ListView- Item-Click? 将“标题”项保存到ListView,然后单击该项以将数据加载到新活动 - Save “Title” item to ListView and click on item to load data to new activity 如何启动基于列表视图项单击的活动? - How to start activity based on listview item click? 如何在Android中的ListView的项目单击上重新加载活动 - how to reload activity on Item click of ListView in android 如何将android活动聚焦到listview的特定项目 - How to focus android activity to the specific item of the listview 长按列表视图项时如何显示意图选择器? - How to display intent chooser when long click listview item? 如何将点击添加到listView中的特定项目 - How to add click to specific item in listView 如何将特定值加载到列表视图的ViewHolder项目? - How to load specific values to a ViewHolder item of a listview? 如何将特定项目故意放入ListView中? - How do I put a specific item in my ListView in an intent? 如何将意图用于列表视图适配器中的每个项目,以用于包含列表视图的不同活动? - How to use intent for each item in listview adapter to a different activity which contains a listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM