如何使我的ListView项目可点击?

时间:2013-01-05 18:52:11

标签: android listview

我在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);
            }
        });
    }

}

2 个答案:

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

答案 1 :(得分:2)

为列表视图设置ONItemSelectedListener或ONItemClickListener。

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
    }

});