简体   繁体   中英

How to go Another Activity from Android ListView

I have Three Activity, such as; Android.Java, Windows.Java, Apple.Java, Blackberry.Java

When I click Android, Windows, Apple, Blackberry item on Listview, I Want to go these Activity.

How can I go these Activity below code.

Here my Main_Activity.Java Code

 package com.nasir.bd; import java.util.ArrayList; 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.ListView; import android.widget.Toast; import com.nasir.lv.EntryAdapter; import com.nasir.lv.EntryItem; import com.nasir.lv.Item; import com.nasir.lv.SectionItem; import com.sunil.sectionlist.R; public class Main_Activity extends Activity implements OnItemClickListener{ ArrayList<Item> items = new ArrayList<Item>(); ListView listview=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview=(ListView)findViewById(R.id.listView_main); items.add(new SectionItem("Mobile Version")); items.add(new EntryItem(R.drawable.survey_g, "Android OS", "Lolipop", "android 5.1", "2014", "Google")); items.add(new EntryItem(R.drawable.survey_g, "Windows OS", "Lumia", "Windows Phone 8.1", "2014", "Microsoft")); items.add(new EntryItem(R.drawable.survey_g, "Apple iOS", "iPhone", "iOS 8", "2015", "Apple")); items.add(new EntryItem(R.drawable.survey_g, "Blackberry", "Blackberry", "Blackberry 7.1", "2012", "Blackberry")); EntryAdapter adapter = new EntryAdapter(this, items); listview.setAdapter(adapter); listview.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { EntryItem item = (EntryItem)items.get(position); Toast.makeText(this, "You clicked " + item.Designation , Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Main_Activity.this, Android.class); startActivity(intent); } } 

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

    EntryItem item = (EntryItem)items.get(position);
    Toast.makeText(this, "You clicked " + item.Designation , Toast.LENGTH_SHORT).show();
    if(position = 0){
       Intent intent = new Intent(Main_Activity.this, Android.class);
       startActivity(intent);
    }else if(position = 1){
       Intent intent = new Intent(Main_Activity.this, Windows.class);
       startActivity(intent);
    }else if(position = 2){
       Intent intent = new Intent(Main_Activity.this, Apple.class);
       startActivity(intent);
    }else if(position = 3){
       Intent intent = new Intent(Main_Activity.this, Blackberry.class);
       startActivity(intent);
    }
}

Hope it works.!

Your code can be changed depending on the position how the list elements are added:

   @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int    position, long arg3)  
    {

    EntryItem item = (EntryItem)items.get(position);
    Toast.makeText(this, "You clicked " + item.Designation , Toast.LENGTH_SHORT).show();
    if(position==1)
    {
      Intent intent = new Intent(Main_Activity.this, Android.class);
      startActivity(intent);
    }
    if(position==2)
    {
      Intent intent = new Intent(Main_Activity.this, Apple.class);
      startActivity(intent);
    }
}

and so on

You could do an if statement or something similar?

if(item.Designation.equals("Windows")
{
    Intent intent = new Intent(Main_Activity.this, windows.class);
}
else if(item.Designation.equals("apple")
{
    Intent intent = new Intent(Main_Activity.this, apple.class);
}
else if(item.Designation.equals("blackberry")
{
    Intent intent = new Intent(Main_Activity.this, blackberry.class);
}
else if(item.Designation.equals("android")
{
    Intent intent = new Intent(Main_Activity.this, android.class);
}

startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM