1

嗨,伙计们,在尝试了一些你们建议的不同代码后,我改变了代码的工作方式。现在我在主屏幕上有 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);
}


}

res/layout 中的 acivity_home_list.xml 文件

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

res/values 中的 strings.xml 文件

<?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>
4

2 回答 2

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  
   }
 }
于 2013-02-19T10:27:34.443 回答
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);

        }}
于 2013-02-19T11:03:21.383 回答