我无法启动新活动,点击列表视图中的项目。我希望onItemClick
可以打开ApkInfoActivity
。实际上当我点击什么都没有发生时。
protected void onItemClick(ListView l, View v, int position, long id, AdapterView<?> parent) {
super.onListItemClick(l, v, position, id);
final ApplicationInfo app = applist.get(position);
PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position);
AppDataActivity appData = (AppDataActivity) getApplicationContext();
appData.setPackageInfo(packageInfo);
Intent appInfo = new Intent(getApplicationContext(), ApkInfoActivity.class);
startActivity(appInfo);
}
我找不到问题。我怎么解决?
使用logcat编辑:
10-29 17:14:07.710: E/AndroidRuntime(3535): FATAL EXCEPTION: main
10-29 17:14:07.710: E/AndroidRuntime(3535): java.lang.ClassCastException: android.content.pm.ApplicationInfo cannot be cast to android.content.pm.PackageInfo
10-29 17:14:07.710: E/AndroidRuntime(3535): at com.dd.application.MainActivity.onItemClick(MainActivity.java:369)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.widget.AdapterView.performItemClick(AdapterView.java:297)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.widget.AbsListView.performItemClick(AbsListView.java:1149)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2939)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.widget.AbsListView$2.run(AbsListView.java:3622)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.os.Handler.handleCallback(Handler.java:730)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.os.Looper.loop(Looper.java:137)
10-29 17:14:07.710: E/AndroidRuntime(3535): at android.app.ActivityThread.main(ActivityThread.java:5323)
10-29 17:14:07.710: E/AndroidRuntime(3535): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 17:14:07.710: E/AndroidRuntime(3535): at java.lang.reflect.Method.invoke(Method.java:525)
10-29 17:14:07.710: E/AndroidRuntime(3535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:743)
10-29 17:14:07.710: E/AndroidRuntime(3535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-29 17:14:07.710: E/AndroidRuntime(3535): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:18)
用它来做你的工作
list.setOnItemClickListener(new AdapterView.onItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Intent appInfo = new Intent(YourActivity.this, ApkInfoActivity.class);
startActivity(appInfo);
}
});
答案 1 :(得分:10)
public class MenuYangu extends ListActivity {
String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menuone.this,
android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if (position == 0) {
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}
else if (position == 2) {
Intent intent = new Intent(this, FriendList.class);
startActivity(intent);
}
}
}
}
答案 2 :(得分:1)
您需要使用Intent,您还可以将点击的listview项数据传递给新活动。
String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),ApkInfoActivity.class);
intent.putExtra("name",classes[i]);
startActivity(intent);
}
});
}
输出:
您可以找到整个教程here
答案 3 :(得分:0)
为Listview添加setOnItemclickListener()。
答案 4 :(得分:0)
使用此:
Intent appInfo = new Intent(CurrentActivity.this, ApkInfoActivity.class);
startActivity(appInfo);
答案 5 :(得分:0)
尝试将方法标题从protected
更改为public
。
修改
现在我看一下,你的方法标题实际上是错误的。它应该如下:
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
变量必须与它们实现的接口中的变量顺序相同。
答案 6 :(得分:0)
例如,如果你想打开一个基于文本的活动你点击列表视图,即如果“abcd”是点击列表视图的选项,你想打开同名“abcd”的活动,那么执行此.. ..
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String temp=yourarray[position];
try{
Class myclass=Class.forName("yourpackagename."+temp);
Intent in=new Intent(this,myclass);
startActivity(in);
}catch(Exception e){
}
}
答案 7 :(得分:0)
解释我的答案。我假设您已按照发布的代码顺序设置列表视图。我只会查看您的代码的这一部分: super.onListItemClick(l,v,position,id); 我不这是必要的。 在我给出的例子的情况下:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
if(position==0){
Intent appInfo = new Intent(SwahiliService.this, DisplayActivity.class);
startActivity(appInfo);
}
if(position==1){
Intent english=new Intent(SwahiliService.this,EnglishService.class);
startActivity(english);
}
if(position==2){
Toast.makeText(getApplicationContext(),"You have selected pst3", Toast.LENGTH_LONG).show();
}
我只是在列表视图中设置一个lister,我称之为lv, 我的适配器(我的listview项的持有者)设置了三个变量,一个是View,int是位置,long是参数: 我指的是列表视图中选择的项目,其位置通常从0开始(尽管您可以将其实例化为任何其他数字,因为您希望e,g int position = 1,将项目计数开始为1)。 从这里你可以使用任何控件结构来按照单击的项目启动活动,在我的情况下,我使用了for循环,因为我假设我的listview只有三个项目,对于更大的listview项目,你可以使用for循环。 请注意我如何通过首先引用当前活动来开始我的新活动,如下所示(SwahiliService.this)可以安全地替换为(仅此关键字),然后遵循我想要开始的活动。 我希望现在更加精细。
答案 8 :(得分:0)
//添加ArrayList和ArrayAdapter:
final ArrayList<String> listItems = new ArrayList<String>();
listItems.add("image_one");
listItems.add("image_two");
listItems.add("image_three");
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listItems);
myListView.setAdapter(myArrayAdapter);
//添加类的ArrayList :
final ArrayList<Class> intents = new ArrayList<Class>();
intents.add(image_one.class);
intents.add(image_two.class);
intents.add(image_three.class);
//单击列表项以从类的ArrayList :
打开类 myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent listIntent = new Intent(getApplicationContext(),
intents.get(position));
startActivity(listIntent);
}
});
答案 9 :(得分:-1)
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
if(position==0){
Intent appInfo = new Intent(SwahiliService.this, DisplayActivity.class);
startActivity(appInfo);
}
if(position==1){
Intent english=new Intent(SwahiliService.this,EnglishService.class);
startActivity(english);
}
if(position==2){
Toast.makeText(getApplicationContext(),"You have selected pst3", Toast.LENGTH_LONG).show();
}
}});