Android: onListItemClick in Activity

前端 未结 8 1983
故里飘歌
故里飘歌 2020-12-28 21:23

Previous time I asked a question here I learned a lot so I guess it\'s worth a shot to try it again.

I am using the lazy list by Fedor from this link: Lazy load of i

相关标签:
8条回答
  • 2020-12-28 21:49

    A listItemClickListener is attached to a ListView. When you changed ListActivity to Activity, your class no longer has a view associated with it and thus an Activity class has no idea what to do with an onListItemClickListener.

    You just have to attached a listener to your ListView:

    listview.setOnItemClickListener(new OnItemClickListener(){
        @Override
        protected void onListItemClick(AdapterView<?> parent, View view, int position, long id){
            //Do stuff
        }
    });
    
    0 讨论(0)
  • 2020-12-28 21:54

    Assume you have datasource Fruit contain few strings. You can define the onCreate() as following:

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,
                    FRUITS)); // context is this, style is list_fruit, Context, ,data_binding to FRUITs
    

    And then write the onListItemClick() as following:

    protected void onListItemClick(ListView parent, View v, int position, long id) {
                    Toast.makeText(this,  " You selected  " + FRUITS[position], Toast.LENGTH_SHORT).show();
            }
    

    I hope it works for you :)

    0 讨论(0)
提交回复
热议问题