事件单击ListView(扩展ListActivity)

时间:2011-05-31 14:12:07

标签: java android expandablelistview

我有这个代码,但是当我点击列表中的某个项目时,我看不到该动作。 这段代码向我展示了ArrayList profilesArrayList的信息,但我不知道我如何检查我从listview中选择的项目。有人可以帮帮我吗?

profilesArrayList = new ArrayList<Profile>();
    profilesArrayList = copyProfilesToArrayList();
    ProfileAdapter adapter = new ProfileAdapter(
            getApplicationContext(), R.layout.profiles_item, profilesArrayList);

    listViewProfiles = (ListView)findViewById(android.R.id.list);
    listViewProfiles.setAdapter(adapter);

    listViewProfiles.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            switch(position) {
            case 0:

                Log.d("cardNumber", profilesArrayList.get(0).getCardNumber());

                break;

2 个答案:

答案 0 :(得分:0)

如果此代码的容器类是ListActivity,则只需覆盖该类的onListItemClick,而不是将其设置为视图的OnItemClickListener。这对我有用

public class ProfileList extends ListActivity
{
    private ArrayList<Profile> profilesArrayList;
    @Override 
    public void onCreate ( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        //populate your arraylist

        setListAdapter ( new ArrayAdapter<Profile>() );
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        Log.d("cardNumber", profilesArrayList.get(position).getCardNumber());
    }
}

答案 1 :(得分:0)

尝试使用getSelectedItemPosition()并添加INVALID_POSITION作为您可能的案例之一