Android开始新的活动

时间:2013-06-11 23:28:21

标签: android

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

JAVA CODE

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 CODE

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

3 个答案:

答案 0 :(得分: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();
                }
            });
    }

答案 1 :(得分:2)

您可以致电setOnItemClickListener上的list并查看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...
        }
    }
});

答案 2 :(得分: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();

        }
    }
});