How can i start a different activity on item click from a custom listview?

前端 未结 4 1285
不思量自难忘°
不思量自难忘° 2021-01-14 13:03

i followed numerous tuitorials like http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html Also followed the questions asked here How to make custom

4条回答
  •  不思量自难忘°
    2021-01-14 13:18

    Your code is running fine. I made it without Customadapter. All you need is below.

    MainActivity.java

    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 adapter = new ArrayAdapter(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

    
    
       
       
    
     
    

    activity_listview.xml

    
    
    
    
    

    AndroidManifest.xml

    
    
    
    
    
    
        
            
                
    
                
            
        
         
    
    

提交回复
热议问题