如何去对列表项的特定活动点击? [英] How to go to a particular activity on list item click?

查看:126
本文介绍了如何去对列表项的特定活动点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发Android的应用程序中,我创建几个项目的列表视图。 我想打电话给一个类上的每个列表项click.Here是我的code

 进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;

进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.Toast;
进口com.example.listview.abc;

公共类列表视图扩展了活动实施OnItemClickListener {
    公众的ListView LV1;
    公共字符串[]名称= {ABC,某某,焊接工艺评定,日本鬼子,周杰伦,
            喇嘛喇嘛};

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        LV1 =(ListView控件)findViewById(R.id.list);
        lv1.setAdapter(新ArrayAdapter<字符串>(这一点,
                android.R.layout.simple_list_item_1,名称));
        lv1.setOnItemClickListener(本);
    }


    @覆盖
    公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
         如果(位置== 0){
             意图I =新的意图(这一点,abc.class);
             startActivity(ⅰ);
            //Toast.makeText(this,你pressed在列表中的第一项,
                // Toast.LENGTH_SHORT).show();
        } 其他 {
            Toast.makeText(这一点,你pressed列表中的所有其他项目,
                    Toast.LENGTH_SHORT).show();

        }
         //否则,如果(位置== 1){
            //这里开始另一项活动...
    }
 

现在的问题是,当我点击它崩溃的第一个项目,但在其他项目点击显示信息,但是当我点击了下一个活动项目崩溃 我试图按照计算器的这个问题,但没有奏效 { <一href="http://stackoverflow.com/questions/4582757/how-to-jump-from-listview-to-next-activityclass-in-android">How从ListView中跳转到下一个活动 } 这里是日志的猫的某些部分。

  12月6日至九日:29:52.111:DEBUG / AndroidRuntime(903):关闭虚拟机
十二月六日至九号:29:52.181:信息/ AndroidRuntime(903):注:连接线的捆扎线#3'失败
十二月六日至九号:29:52.181:信息/ ActivityManager(60):启动PROC com.example.listview的活动com.example.listview / .listview:PID = 910 UID = 10039导报= {}
十二月六日至九号:29:52.310:DEBUG / dalvikvm(903):调试器分离;对象注册了1项
十二月六日至九号:29:54.670:信息/ ActivityManager(60):显示活动com.example.listview / .listview:2616毫秒(共199904毫秒)
十二月六日至九号:29:55.249:DEBUG / dalvikvm(253):GC_EXPLICIT释放176对象/在3461ms 12384字节
十二月六日至九号:29:59.940:DEBUG / dalvikvm(125):GC_EXPLICIT释放878对象/ 48664字节188ms
十二月六日至九号:30:05.270:DEBUG / dalvikvm(262):GC_EXPLICIT释放46的对象/ 2240字节183ms
十二月六日至九号:30:50.140:DEBUG / SntpClient(60):请求时失败:产生java.net.SocketException:地址族不支持的协议
十二月六日至九号:32:52.731:DEBUG / dalvikvm(120):GC_FOR_MALLOC释放9850对象/在1222ms 473648字节
十二月六日至九号:35:50.171:DEBUG / SntpClient(60):请求时失败:产生java.net.SocketException:地址族不支持的协议
十二月六日至九号:40:50.240:DEBUG / SntpClient(60):请求时失败:产生java.net.SocketException:地址族不支持的协议
十二月六日至九号:45:50.311:DEBUG / SntpClient(60):请求时失败:产生java.net.SocketException:地址族不支持的协议
 

而menifest文件是

**

  

**

 &LT; XML版本=1.0编码=UTF-8&GT?;
&GT; &LT;舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
&GT;包=com.example.listview安卓版code =1
&GT;机器人:VERSIONNAME =1.0&GT;
&GT; &lt;应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME&GT;
&GT; &LT;活动机器人:列表视图名称=机器人:标签=@小号特林/ APP_NAME&GT;
&GT;
&GT; &LT;意向滤光器&gt;
&GT; &lt;作用机器人:名称=android.intent.action.MAIN/&GT;
&GT; &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
&GT; &所述; /意图滤光器&gt;
&GT; &LT; /活性GT;
&GT;
&GT; &LT;活动的android:启用=真正的机器人:NAME =ABC/&GT;
&GT; &LT; /用途&gt;
&GT; &LT;使用-SDK安卓的minSdkVersion =3/&GT;
&GT; &LT;使用-权限的Andr​​oid:名称=android.permission.INTERNET对/&GT;
&GT; &LT; /舱单&GT;
 

**

**

请看看在code和帮助我

在此先感谢。

解决方案

你有没有正确注册 abc.class 在你的Andr​​oidManifest.xml文件中的活动?这将导致崩溃。

I am developing an application in android in which i creating a list view of few items. I want to call a class on each list item click.Here is my code

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.listview.abc;

public class listview extends Activity implements OnItemClickListener {
    public ListView lv1;
    public String[] names = { "Abc", "Xyz", "Pqr", "Jap", "Jay",
            "bla bla" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lv1 = (ListView) findViewById(R.id.list);
        lv1.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, names));
        lv1.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
         if(position==0){
             Intent i = new Intent(this, abc.class);
             startActivity(i);
            //Toast.makeText(this, "You pressed the first item in the list",
                //  Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "You pressed all other items in the list",
                    Toast.LENGTH_SHORT).show();

        }
         // else if(position==1){
            // start another activity here...
    }

The problem is that when i click the first item it crashes but the click on other item shows the message but when i click on the item for next activity it crashes i tried to follow this question of stackoverflow but it didn't work { How to jump from ListView to next Activity } here is some part of log cat

06-09 12:29:52.111: DEBUG/AndroidRuntime(903): Shutting down VM
06-09 12:29:52.181: INFO/AndroidRuntime(903): NOTE: attach of thread 'Binder Thread #3' failed
06-09 12:29:52.181: INFO/ActivityManager(60): Start proc com.example.listview for activity com.example.listview/.listview: pid=910 uid=10039 gids={}
06-09 12:29:52.310: DEBUG/dalvikvm(903): Debugger has detached; object registry had 1 entries
06-09 12:29:54.670: INFO/ActivityManager(60): Displayed activity com.example.listview/.listview: 2616 ms (total 199904 ms)
06-09 12:29:55.249: DEBUG/dalvikvm(253): GC_EXPLICIT freed 176 objects / 12384 bytes in 3461ms
06-09 12:29:59.940: DEBUG/dalvikvm(125): GC_EXPLICIT freed 878 objects / 48664 bytes in 188ms
06-09 12:30:05.270: DEBUG/dalvikvm(262): GC_EXPLICIT freed 46 objects / 2240 bytes in 183ms
06-09 12:30:50.140: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:32:52.731: DEBUG/dalvikvm(120): GC_FOR_MALLOC freed 9850 objects / 473648 bytes in 1222ms
06-09 12:35:50.171: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:40:50.240: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:45:50.311: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol

And the menifest File is

**

**

<?xml version="1.0" encoding="utf-8"?>
>     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
>       package="com.example.listview" android:versionCode="1"
>       android:versionName="1.0">
>       <application android:icon="@drawable/icon" android:label="@string/app_name">
>           <activity android:name=".listview" android:label="@s tring/app_name">
>           
>               <intent-filter>
>                   <action android:name="android.intent.action.MAIN" />
>                   <category android:name="android.intent.category.LAUNCHER" />
>               </intent-filter>
>           </activity>
>     
>           <activity android:enabled="true" android:name="abc" />
>       </application>
>       <uses-sdk android:minSdkVersion="3" />
>            <uses-permission android:name="android.permission.INTERNET" />
>     </manifest>

**

**

Please have a look at the code and help me out

Thanks in advance..

解决方案

Did you correctly register abc.class as an Activity in your AndroidManifest.xml file? That would cause a crash.

这篇关于如何去对列表项的特定活动点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆