如何从Android ListView

时间:2015-10-20 12:45:01

标签: java 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);
	}
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

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

希望它有效。!

答案 1 :(得分:0)

您的代码可以更改 取决于列表元素的添加位置:

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

等等

答案 2 :(得分:0)

你可以做一个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);