android - How can I make items inside the array adapter clickable? -
basically i'm trying make view open item tapped inside array adapter. states need use
setonitemclicklistener()
i'm not sure need put method works listview.
package com.icemalta.dylan.memorybuddy; import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.arrayadapter; import java.text.parseexception; import java.util.arraylist; import java.util.date; import java.text.simpledateformat; public class notelist extends listactivity { private static final int add_note_request = 10; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_note_list); loadnotes(); } private void loadnotes() { // arraylist<note> notes = storagehelper.loadnotes(this); arraylist<note> notes = new memorybuddycontract.memorybuddydbhelper(this).getnotes(); if (notes.size() > 0) { note.notes = notes; arrayadapter notesadapter = new arrayadapter( this, android.r.layout.simple_list_item_1, note.notes ); this.setlistadapter(notesadapter); } } public void showaddnoteview(view v) { intent intaddnote = new intent(this, addnote.class); this.startactivityforresult(intaddnote, add_note_request); } public void showviewnoteview(view v) { intent intviewnote = new intent(this, addnote.class); this.startactivityforresult(intviewnote, add_note_request); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == add_note_request && resultcode == result_ok) { string title = data.getstringextra("title"); string desc = data.getstringextra("desc"); string datestring = data.getstringextra("date"); try { date date = new simpledateformat("d/m/yyyy h:m").parse(datestring); note n = new note(title, desc, date); //storagehelper.savenotes(this, note.notes); new memorybuddycontract.memorybuddydbhelper(this).addnote(n); arrayadapter<note> noteadapater = new arrayadapter( this, android.r.layout.simple_list_item_1, note.notes ); this.setlistadapter(noteadapater); } catch (parseexception e) { e.printstacktrace(); } } } }
just create custom adapter listitem , can use click listener,try way
public class myadapter extends arrayadapter<string> { context context; int layoutresourceid; arraylist<string> data = null; weatherholder holder; public myadapter(context context, int layoutresourceid, arraylist<string> data) { // super(context, layoutresourceid, data, coeff); super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } public view getview(int position, view convertview, viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = ((activity) context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new weatherholder(); holder.name = (textview) row.findviewbyid(r.id.item_cours_name); holder.b = (imagebutton) row.findviewbyid(r.id.button); holder.b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub system.out.println("clicked"); } }); row.settag(holder); } else { holder = (weatherholder) row.gettag(); } holder.b.settag(holder); string name1 = data.get(position); holder.name.settext(name1); return row; } static class weatherholder { textview name; imagebutton b; } }
Comments
Post a Comment