繁体   English   中英

如何从Android ListView进行其他活动

[英]How to go Another Activity from Android ListView

我有三个活动,例如; Android.Java,Windows.Java,Apple.Java,Blackberry.Java

当我单击Listview上的Android,Windows,Apple,Blackberry项目时,我要执行这些活动。

如何在代码下面进行这些活动。

这是我的Main_Activity.Java代码

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

希望它有用。

您可以根据添加列表元素的位置来更改代码:

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

等等

你可以做一个if语句或类似的东西吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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