首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从自定义列表视图开始项目单击上的不同活动?

如何从自定义列表视图开始项目单击上的不同活动?
EN

Stack Overflow用户
提问于 2014-11-10 11:36:36
回答 4查看 20.5K关注 0票数 3

我跟踪了许多教程,比如http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html,也跟踪了这里询问的问题,但在尝试了这里的答案(How to make custom ListView to open other activities when clicking list item?)之后,每当我从列表视图中选择一个项目时,我的应用程序就会停止运行。我的主要活动代码:

代码语言:javascript
复制
public class MainActivity extends Activity {
	  ListView list;
	  String[] web = {
			  "Notifications",
			  "School",
			  "What's Hot",
			  "Tell a friend",
			  "Hit us up",
			  "Settings",
			  "About & Help"
	  };
@Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    CustomList adapter = new
	        CustomList(MainActivity.this, web, imageId);
	    list=(ListView)findViewById(R.id.list);
	        list.setAdapter(adapter);
	        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	        	@Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
	        		switch(position){
	        		case 0:  Intent newActivity = new Intent(MainActivity.this, Notifications.class);     
	                startActivity(newActivity);
	                break;
	       case 1:  Intent newActivity1 = new Intent(MainActivity.this, School.class);     
	                startActivity(newActivity1);
	                break;
	       case 2:  Intent newActivity2 = new Intent(MainActivity.this, Whats_hot.class);     
	                startActivity(newActivity2);
	                break;
	       case 3:  Intent newActivity3 = new Intent(MainActivity.this, Tellafriend.class);     
	                startActivity(newActivity3);
	                break;
	       case 4:  Intent newActivity4 = new Intent(MainActivity.this, Hitusup.class);     
	                startActivity(newActivity4);
	                break;
	       case 5:  Intent newActivity5 = new Intent(MainActivity.this, Settings.class);     
           startActivity(newActivity5);
           break;
  case 6:  Intent newActivity6 = new Intent(MainActivity.this, AboutHelp.class);     
           startActivity(newActivity6);
           break;
  
	        	 }
	        	
	        	}
	        @SuppressWarnings("unused")
	        public void onClick(View v){ 
      };
    });}
}

下面是其中一项活动(学校)的代码:

代码语言:javascript
复制
public class School extends Activity{
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
	
	
	Intent newActivity1=new Intent();
	setResult(RESULT_OK, newActivity1);
    finish();
	}

}

在我的舱单上我补充说:

代码语言:javascript
复制
</activity>
        <activity android:name=".Notifications"></activity>
            <activity android:name=".School"></activity>
                <activity android:name=".Whats_hot"></activity>
                    <activity android:name=".Tellafriend"></activity>
                        <activity android:name=".Hitusup"></activity>
                            <activity android:name=".Settings"></activity>
                                <activity android:name=".AboutHelp">

我的日志:11-1014:25:58.080: W/dalvikvm(13150):拒绝重新启动DEX‘/system/framework/hwfrawork.jar’11-10 14:25:59.360: I/Adreno200-EGL(13150):EGL1.4QualcommBuild: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010_msm8625_JB_REL_2.0.3_Merge_release_AU (合并)11-1014:25:59。I/Adreno200-EGL(13150):构建日期: 10/26/12 Fri 11-10 14:25:59.360: I/Adreno200-EGL(13150):本地分部:11-1014:25:59.360: I/Adreno200-EGL(13150):远程分支:quic/jb_rel_2.0.311-1014:25:59.360: I/Adreno200-EGL(13150):本地补丁:NO11-10 14:25:59.360: 59.360: I/Adreno200-EGL(13150 )):重构分支: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010 + NOTHING 11-10 14:26:00.450: I/编舞师(13150):跳过90帧!应用程序可能在其主线程上做了太多的工作。11-10 14:26:03.230: W/dalvikvm(13150):threadid=1:线程退出与未明异常(group=0x413fe438)

有些地方不对劲,我对android的开发并不熟悉,如果你们能提供帮助,我会非常感激的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-11-10 15:54:34

你的代码运行良好。我在没有定制适配器的情况下成功了。你所需要的就是下面。

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends Activity {


    String[] web = {
              "Notifications",
              "School",
              "What's Hot",
              "Tell a friend",
              "Hit us up",
              "Settings",
              "About & Help"
      };

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_mainlist);

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.activity_listview, web);

      ListView list = (ListView) findViewById(R.id.list);
      list.setAdapter(adapter);

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                switch(position){
                case 0:  Intent newActivity = new Intent(MainActivity.this, School.class);     
                startActivity(newActivity);
                break;


             }

            }
        @SuppressWarnings("unused")
        public void onClick(View v){ 
            };
        });

   }//end oncreate


}//endactivity

School.java

代码语言:javascript
复制
public class School extends Activity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.school);


    Intent newActivity1=new Intent();
    setResult(RESULT_OK, newActivity1);
    //finish();
}

}

activity_mainlist.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListActivity" >

   <ListView
   android:id="@+id/list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >
   </ListView>

 </LinearLayout>

activity_listview.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

AndroidManifest.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zzztest2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.example.zzztest2.School" android:label="@string/app_name"> </activity>
</application>

票数 5
EN

Stack Overflow用户

发布于 2016-03-10 13:08:43

代码语言:javascript
复制
public class MainActivity extends Activity {
	  ListView list;
	  String[] web = {
			  "Notifications",
			  "School",
			  "What's Hot",
			  "Tell a friend",
			  "Hit us up",
			  "Settings",
			  "About & Help"
	  };
@Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    CustomList adapter = new
	        CustomList(MainActivity.this, web, imageId);
	    list=(ListView)findViewById(R.id.list);
	        list.setAdapter(adapter);
	        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	        	@Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
	        		switch(position){
	        		case 0:  Intent newActivity = new Intent(MainActivity.this, Notifications.class);     
	                startActivity(newActivity);
	                break;
	       case 1:  Intent newActivity1 = new Intent(MainActivity.this, School.class);     
	                startActivity(newActivity1);
	                break;
	       case 2:  Intent newActivity2 = new Intent(MainActivity.this, Whats_hot.class);     
	                startActivity(newActivity2);
	                break;
	       case 3:  Intent newActivity3 = new Intent(MainActivity.this, Tellafriend.class);     
	                startActivity(newActivity3);
	                break;
	       case 4:  Intent newActivity4 = new Intent(MainActivity.this, Hitusup.class);     
	                startActivity(newActivity4);
	                break;
	       case 5:  Intent newActivity5 = new Intent(MainActivity.this, Settings.class);     
           startActivity(newActivity5);
           break;
  case 6:  Intent newActivity6 = new Intent(MainActivity.this, AboutHelp.class);     
           startActivity(newActivity6);
           break;
  
	        	 }
	        	
	        	}
	        @SuppressWarnings("unused")
	        public void onClick(View v){ 
      };
    });}
}

票数 1
EN

Stack Overflow用户

发布于 2016-07-01 10:44:57

通过这样做,您不必为活动编写所有的开关大小写。

代码语言:javascript
复制
public class MainActivity extends Activity {
  ListView list;
  String[] web = {
          "Notifications",
          "School",
          "What's Hot",
          "Tell a friend",
          "Hit us up",
          "Settings",
          "About & Help"
  };
  String[] s1 = {
          "Notifications",
          "School",
          "Whats_hot",
          "Tellafriend",
          "Hitusup",
          "Settings",
          "AboutHelp"
  };


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CustomList adapter = new
    CustomList(MainActivity.this, web, imageId);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String a=s1[position];
                Class a1= null;
                try {
                     a1 =    Class.forName("<Your Package name>."+a);
                } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                }
                startActivity(new Intent(MainActivity.this,a1));

            }
        @SuppressWarnings("unused")
        public void onClick(View v){ 
        };
     });
  }

}

别忘了说‘’。就在你包裹的名字后面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26842916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档