我有此代码,但是单击列表中的项目时看不到该操作。
这段代码向我显示了有关ArrayListprofilesArrayList的信息,但我不知道如何检查我从列表视图中选择的项目。有人可以帮助我吗?

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;

最佳答案

如果此代码的容器类为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());
    }
}

关于java - 事件单击ListView(扩展ListActivity),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6188873/

10-10 15:10