如何在Listview(项目)上加载特定活动(Intent)单击?

时间:2013-09-23 10:55:25

标签: android listview android-intent

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

   }

在上面的程序..正常情况,如string.equalsIgnorecase(“AA”)  我们可以启动特定点击的意图!!是否有任何方法在任何列表视图中启动活动项目单击???

3 个答案:

答案 0 :(得分:0)

创建类数组因此您可以轻松指向精确的活动...

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

答案 1 :(得分:0)

您可以使用以下代码:

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

   }  

答案 2 :(得分:0)

尝试运行以下代码。

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

}