How can I start another activity on clicking an item from a custom list?

i followed numerous tuitorials like http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html Also followed by questions asked here How to set up a custom ListView to open other actions when clicking on list item? However, after checking the answers here ( How do I get a custom ListView to open other actions when a list item is clicked? ), My app keeps stopping when I select an item from my list. My main activity code:

public class MainActivity extends Activity {
	  ListView list;
	  String[] web = {
			  "Notifications",
			  "School",
			  "What 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){ 
      };
    });}
}
      

Run codeHide result


Here is the code for one of the activities (School) I'm trying to run:

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();
	}

}
      

Run codeHide result


In my manifest, I added the following:

</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">
            
      

Run codeHide result


My logarithm is: 11-10 14: 25: 58.080: W / dalvikvm (13150): refusing to reopen DEX download '/system/framework/hwframework.jar' 11-10 14: 25: 59.360: I / Adreno200-EGL ( 13150) :: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010_msm8625_JB_REL_2.0.3_Merge_release_AU (Merge) 11-10 14: 25: 59.360: I / Adreno200-EGL (1026/12): Build Date Fri 11-10 14: 25: 59.360: I / Adreno200-EGL (13150): Local branch: 11-10 14: 25: 59.360: I / Adreno200-EGL (13150): remote branch: quic / jb_rel_2.0.3 11- 10 14: 25: 59.360: I / Adreno200-EGL (13150): local fixes: NONE 11-10 14: 25: 59.360: I / Adreno200-EGL (13150): Rebuild branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.01.21.010 + NOTHING 11-10 14: 26: 00.450: I / Choreographer (13150): missed 90 frames!The app might be doing too much work on its main thread. 11-10 14: 26: 03.230: W / dalvikvm (13150): threadid = 1: Exit thread with uncaught exception (group = 0x413fe438)

There is something wrong that I cannot see. I am new to android development and any help from you guys would be greatly appreciated.

+3


source to share


4 answers


Your code is working fine. I did it without Customadapter. Everything you need is below.

MainActivity.java

public class MainActivity extends Activity {


    String[] web = {
              "Notifications",
              "School",
              "What 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

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



<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

<?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

<?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


source




public class MainActivity extends Activity {
	  ListView list;
	  String[] web = {
			  "Notifications",
			  "School",
			  "What 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){ 
      };
    });}
}
      

Run codeHide result


+1


source


This way you don't need to record the entire switch event for the activity.

public class MainActivity extends Activity {
  ListView list;
  String[] web = {
          "Notifications",
          "School",
          "What 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){ 
        };
     });
  }

}

      

Remember to put a period "." Right after your package name.

+1


source


public class Menu extends ListActivity {
    String classes[]={"startingPoint","TextPlay","TextPlayPerfectJavaCode","EXAMPLE2","example3","example4"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1,classes);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String className=classes[position];
    Class ourClass=null;
    try{
        ourClass=Class.forName("travis.thenewboston.com.thenewboston."+className);
        Intent ourIntent = new Intent(getApplicationContext(), ourClass);//replacing Menu.this - getApplicationContext()
        startActivity(ourIntent);
    }
    catch (ClassNotFoundException e){
        e.printStackTrace();
    }
    finally {
        Toast.makeText(Menu.this, "Clicked at Position: "+Integer.toString(position), Toast.LENGTH_SHORT).show();
    }
}

      

}

0


source







All Articles