1
 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") 这样的正常条件,我们可以在特定点击上启动意图!是否有任何方法可以在任何列表视图项目单击中启动活动???

4

3 回答 3

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);
}
于 2013-09-23T11:27:44.427 回答
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));

   }  
于 2013-09-23T11:52:17.407 回答
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);

}  
于 2013-09-23T12:03:54.770 回答