嗨,大家在尝试了一些你建议的几个不同的代码失败后,我改变了代码的工作方式。现在我在主屏幕上有3个列表项,应该调用3个不同的活动。无论我做什么,这都行不通。 更新07/03/2013 - 非常感谢所有的回复。我已经解决了我遇到的问题。很少有小调整和它的工作。 再次欢呼帮助..
我的主要活动
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);
}
});
}
}
package com.example.mechanicalengineering;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MetalList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_metal_list);
}
}
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeList" >
<ListView
android:id="@+id/HomePageList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/HomePageList" >
</ListView>
</RelativeLayout>
<?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>
答案 0 :(得分:0)
试试这个。您必须将项目添加到主页
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
}
}
答案 1 :(得分:0)
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);
}}