android - Using ListActivity in Fragment -
the below code running custom listview implemented in seperate project.
public class mainactivity extends listactivity implements ontouchlistener{ private mycustomadapter madapter; activity temp = this; string []s = new string[500]; arraylist<gs> q = new arraylist<gs>(); listview lv; int count=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); dbadapter db = dbadapter.getdbadapter(getapplicationcontext()); if (!db.checkdatabase()) { db.createdatabase(getapplicationcontext()); } db.opendatabase(); q = db.getdata(); madapter = new mycustomadapter(); madapter.addseparatoritem(new contentwrapper(q.get(0).geta_name(),null)); madapter.additem(new contentwrapper(q.get(0).getas_name(), q.get(0).getdesc_art())); (int = 1; < 460; i++) { if (!(q.get(i).geta_name().trim().equals(q.get(i-1).geta_name().trim()))) { madapter.addseparatoritem(new contentwrapper(q.get(i).geta_name(), null)); } madapter.additem(new contentwrapper(q.get(i).getas_name(), q.get(i).getdesc_art())); } setlistadapter(madapter); } //adapter class private class mycustomadapter extends baseadapter { private static final int type_item = 0; private static final int type_separator = 1; private static final int type_max_count = type_separator + 1; private arraylist<contentwrapper> mdata = new arraylist<contentwrapper>(); private layoutinflater minflater; private treeset<integer> mseparatorsset = new treeset<integer>(); public mycustomadapter() { minflater = (layoutinflater)getsystemservice(context.layout_inflater_service); } public void additem(contentwrapper value) { mdata.add(value); notifydatasetchanged(); } public void addseparatoritem(contentwrapper value) { mdata.add(value); // save separator position mseparatorsset.add(mdata.size() - 1); notifydatasetchanged(); } public contentwrapper getitem(int position) { return mdata.get(position); } @override public int getitemviewtype(int position) { return mseparatorsset.contains(position) ? type_separator : type_item; } @override public int getviewtypecount() { return type_max_count; } public int getcount() { return mdata.size(); } public long getitemid(int position) { log.v("getitemid position", ""+position); return position; } public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; int type = getitemviewtype(position); if (convertview == null) { holder = new viewholder(); switch (type) { case type_item: convertview = minflater.inflate(r.layout.activity_main1, null); holder.textview = (textview)convertview.findviewbyid(r.id.text); break; case type_separator: convertview = minflater.inflate(r.layout.activity_main2, null); holder.textview = (textview)convertview.findviewbyid(r.id.textseparator); count++; break; } convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } holder.textview.settext(mdata.get(position).getitem()); if (type == type_item) { holder.textview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog.builder x = new alertdialog.builder(temp); log.v("position",""+position); x.seticon(r.drawable.ic_launcher) // .settitle(q.get(position-count).getas_name()) .settitle(mdata.get(position).getitem()) // .setmessage(q.get(position-count).getdesc_art()) .setmessage(mdata.get(position).getitemdescription()) .setcancelable(true) .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg, int arg1) { } }); alertdialog = x.create(); a.show(); } }); } else { holder.textview.setonclicklistener(null); } return convertview; } } public static class viewholder { public textview textview; } public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub return false; } }
now want running same in app with fragment. know using fragment = new contentsfragment();
initiates in fragment. should extending listfragment
default constructor & oncreateview(...)
have inflated activities
i new fragments & don't know things should changed in code.
please !
edit:
i showing implemented code of have tried & getting 4 errors have specified in code:
public class contentsfragment extends fragment implements ontouchlistener{ private mycustomadapter madapter; activity temp = this;// error: type mismatch: cannot convert contentsfragment activity string []s = new string[500]; arraylist<gs> q = new arraylist<gs>(); listview lv; int count=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); dbadapter db = dbadapter.getdbadapter(getapplicationcontext());//error :the method getapplicationcontext() undefined type contentsfragment if (!db.checkdatabase()) { db.createdatabase(getapplicationcontext());//error : method getapplicationcontext() undefined type contentsfragment } db.opendatabase(); q = db.getdata(); madapter = new mycustomadapter(); madapter.addseparatoritem(new contentwrapper(q.get(0).geta_name(),null)); madapter.additem(new contentwrapper(q.get(0).getas_name(), q.get(0).getdesc_art())); (int = 1; < 460; i++) { if (!(q.get(i).geta_name().trim().equals(q.get(i-1).geta_name().trim()))) { madapter.addseparatoritem(new contentwrapper(q.get(i).geta_name(), null)); } madapter.additem(new contentwrapper(q.get(i).getas_name(), q.get(i).getdesc_art())); } setlistadapter(madapter); //error : method getapplicationcontext() undefined type contentsfragment } //adapter class private class mycustomadapter extends baseadapter { private static final int type_item = 0; private static final int type_separator = 1; private static final int type_max_count = type_separator + 1; private arraylist<contentwrapper> mdata = new arraylist<contentwrapper>(); private layoutinflater minflater; private treeset<integer> mseparatorsset = new treeset<integer>(); public mycustomadapter() { minflater = (layoutinflater)getsystemservice(context.layout_inflater_service); } public void additem(contentwrapper value) { mdata.add(value); notifydatasetchanged(); } public void addseparatoritem(contentwrapper value) { mdata.add(value); // save separator position mseparatorsset.add(mdata.size() - 1); notifydatasetchanged(); } public contentwrapper getitem(int position) { return mdata.get(position); } @override public int getitemviewtype(int position) { return mseparatorsset.contains(position) ? type_separator : type_item; } @override public int getviewtypecount() { return type_max_count; } public int getcount() { return mdata.size(); } public long getitemid(int position) { log.v("getitemid position", ""+position); return position; } public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; int type = getitemviewtype(position); if (convertview == null) { holder = new viewholder(); switch (type) { case type_item: convertview = minflater.inflate(r.layout.activity_main1, null); holder.textview = (textview)convertview.findviewbyid(r.id.text); break; case type_separator: convertview = minflater.inflate(r.layout.activity_main2, null); holder.textview = (textview)convertview.findviewbyid(r.id.textseparator); count++; break; } convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } holder.textview.settext(mdata.get(position).getitem()); if (type == type_item) { holder.textview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog.builder x = new alertdialog.builder(temp); log.v("position",""+position); x.seticon(r.drawable.ic_launcher) // .settitle(q.get(position-count).getas_name()) .settitle(mdata.get(position).getitem()) // .setmessage(q.get(position-count).getdesc_art()) .setmessage(mdata.get(position).getitemdescription()) .setcancelable(true) .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg, int arg1) { } }); alertdialog = x.create(); a.show(); } }); } else { holder.textview.setonclicklistener(null); } return convertview; } } public static class viewholder { public textview textview; } public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub return false; } }
setlistadapter
method of listfragment
. need extend listfragment
use same.
change this
madapter = new mycustomadapter();
to
madapter = new mycustomadapter(getactivity());
and then
public mycustomadapter(context context) { minflater = layoutinflater.from(context); }
Comments
Post a Comment