3

我对android非常陌生。我正在制作一个列表视图,每次单击列表项时,它都应该打开一个新活动。在这种情况下,设置活动。我尝试了很多方法,但都没有奏效。任何形式的帮助将不胜感激。这是我的代码:

package com.alex.mylist;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class Settings extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        ListView lstSettings = (ListView) findViewById(R.id.lstSettings);
        lstSettings.getChildAt(1).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent mainIntent = new Intent(v.getContext(), Register.class);
                startActivity(mainIntent);
            }
        });
    }

}
4

2 回答 2

7

使用listView.setOnItemClickListener 使 listview 项目可点击

lstSettings.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int i, long l) {

        Intent mainIntent = new Intent(Settings.this,
                                             Register.class);
        startActivity(mainIntent);
        }
    });
于 2013-01-05T18:55:11.747 回答
2

为您的列表视图设置 ItemSelectedListener 或 ItemClickListener。

listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // call Activity
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
    }

});

listView.setOnItemClickListener(new ListView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // call activity
    }

});
于 2013-01-05T18:54:03.660 回答