Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Adding an onclicklistener to listview (android)

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/ but I can't seem to add an onclicklistener I just want to be able to do an action when I click on the row, with the data that the row contains of course any ideas? thanks

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.liste);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Setup the list view
    final ListView prestListView = (ListView) findViewById(R.id.list);
    final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);

    prestListView.setAdapter(prestationAdapterEco);

    // Populate the list, through the adapter
    for(final prestationEco entry : getPrestations()) {
        prestationAdapterEco.add(entry);
    }
    prestListView.setClickable(true);
    prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Object o = prestListView.getItemAtPosition(position);
            String str=(String)o;//As you are using Default String Adapter
            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
        }
    });
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object o = prestListView.getItemAtPosition(position);
        prestationEco str = (prestationEco)o; //As you are using Default String Adapter
        Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...