[英]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.