Android 从listview打开不同的活动
大家好,在尝试了你们中的一些人建议的几种不同的代码失败后,我改变了代码的工作方式。现在我在主屏幕上有3个列表项,它们应该调用3个不同的活动。不管我做什么,这都不起作用。 2013年3月7日更新-非常感谢所有回复。我已经把我遇到的问题整理好了。一些小的调整和它的工作。 再次为你的帮助干杯 我的主要活动Android 从listview打开不同的活动,android,android-layout,listview,android-listview,onclicklistener,Android,Android Layout,Listview,Android Listview,Onclicklistener,大家好,在尝试了你们中的一些人建议的几种不同的代码失败后,我改变了代码的工作方式。现在我在主屏幕上有3个列表项,它们应该调用3个不同的活动。不管我做什么,这都不起作用。 2013年3月7日更新-非常感谢所有回复。我已经把我遇到的问题整理好了。一些小的调整和它的工作。 再次为你的帮助干杯 我的主要活动 package com.example.mechanicalengineering; import android.app.Activity; import android.app.List
package com.example.mechanicalengineering;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class HomeList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_list);
String[] homelist = getResources().getStringArray(R.array.HomePageList);
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_home_list, homelist));
TextView LV = getListView();
LV.setTextFilterEnabled(true);
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = null;
if(((TextView) view).getText().equals("Material Properties - Metal")){
myIntent = new Intent(view.getContext(), MetalList.class);
}
if(((TextView) view).getText().equals("Material Properties - Plastic")){
myIntent = new Intent(view.getContext(), PlasticList.class);
}
if(((TextView) view).getText().equals("Material Properties - Other")){
myIntent = new Intent(view.getContext(), OtherList.class);
}
startActivity(myIntent);
}
});
}
}
res/layout中的acivity\u home\u list.xml文件
res/values格式的strings.xml文件
机械工程
你好,世界!
设置
金属清单
材料特性-金属
材料特性-塑料
材料特性-其他
#3F3F
塑性
其他属性
清单文件
试试这个。您必须将项目添加到homelist中
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class HomeList extends Activity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_list);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//add check if item is the one you need
Intent intent = new Intent( new Intent(this, MetalList.class);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemSelectedListener;
公共类HomeList扩展活动实现了MSelectedListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u home\u列表);
}
@凌驾
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
//添加检查项目是否是您需要的项目
Intent Intent=新Intent(新Intent(this,MetalList.class);
星触觉(意向);
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
}
嘿,就这么做吧
ListView lv=(ListView)findViewById(R.id.HomePageList);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent newwin = new Intent(context, MetalList.class);
startActivity(newwin);
}}
ListView lv=(ListView)findviewbyd(R.id.HomePageList);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
Intent newwin=newintent(上下文,MetalList.class);
星触觉(纽温);
}}
您想什么时候打开第二个活动?是在第一个活动中单击按钮之后还是单击列表项后打开?我想在您选择列表项时打开活动。有一些错误,但我修改了代码,使其包含3个以上的活动,但仍然遇到相同的问题。我已编辑了上面的代码…嘿,将您的全部信息发送给我我会为你做的。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mechanical Engineering</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_metal_list">MetalList</string>
<string-array name="HomePageList">
<item>Material Properties - Metal</item>
<item>Material Properties - Plastic</item>
<item>Material Properties - Other</item>
</string-array>
<color name="divideline">#3f3f3f</color>
<string name="title_activity_plastic_list">Plastic Properties</string>
<string name="title_activity_other_list">Other Properties</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mechanicalengineering"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mechanicalengineering.HomeList"
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.mechanicalengineering.MetalList"
android:label="@string/title_activity_metal_list" >
</activity>
<activity
android:name="com.example.mechanicalengineering.PlasticList"
android:label="@string/title_activity_plastic_list" >
</activity>
<activity
android:name="com.example.mechanicalengineering.OtherList"
android:label="@string/title_activity_other_list" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class HomeList extends Activity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_list);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//add check if item is the one you need
Intent intent = new Intent( new Intent(this, MetalList.class);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
ListView lv=(ListView)findViewById(R.id.HomePageList);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent newwin = new Intent(context, MetalList.class);
startActivity(newwin);
}}