0

我在 xml、android 中创建了一个列表。它的 java 代码具有扩展 Activity 而不是扩展列表活动。当我单击特定行时,我无法编写代码来启动新活动。这是我的代码...

JAVA代码

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends Activity {
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

            ArrayAdapter adapter = new ArrayAdapter<String>(this,``
                android.R.layout.simple_list_item_1, values);
            ListView View = (ListView) findViewById(R.id.list);
            View.setAdapter(adapter);

    }

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="4dp"
android:background="@drawable/ic_launcher"> 

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

</LinearLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="200dp"
    android:layout_height="325dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false" >
</ListView>

</RelativeLayout>
4

3 回答 3

2

尝试这样的事情:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

            ArrayAdapter adapter = new ArrayAdapter<String>(this,``
                android.R.layout.simple_list_item_1, values);
            ListView View = (ListView) findViewById(R.id.list);
            View.setAdapter(adapter);
            view.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    Toast.makeText(getApplicationContext(), "You chose item number: "+position, Toast.LENGTH_LONG).show();
                }
            });
    }
于 2013-06-11T23:41:48.343 回答
2

您可以调用setOnItemClickListenerlist检查position并启动相应的Intent。像这样的东西...

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Check which list item was clicked by checking the 'position' 
        // and start an Activity accordingly
        switch(position) {
        case 0:
            Toast.makeText(getApplicationContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
            Intent oneIntent = new Intent(MainActivity.this, OneFragment.class);
            startActivity(oneIntent);
            break;
        case 1:
            Toast.makeText(getApplicationContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
            Intent twoIntent = new Intent(MainActivity.this, TwoFragment.class);
            startActivity(twoIntent);
        // AND SO ON...
        }
    }
});
于 2013-06-12T01:52:56.050 回答
0
View.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        switch(position) {
        case 0:

            Intent intnscnd = new Intent(MainActivity.this, secondactivity.class);
            startActivity(intnscnd);
            this.finish();
            break;
        case 1:

            Intent intnthrd = new Intent(MainActivity.this, thirdactivity.class);
            startActivity(intnthrd);
            this.finish();

        }
    }
});
于 2013-06-12T04:59:18.660 回答