java - Populating a ListView with Objects from Parse.com Android -
i'm trying fill listview objects database @ parse.com, i'm having trouble because listview not accept objects directly. i've tried making array of strings looping through objects , casting them strings failed. have tried call .tostring on objects no luck either.
my plan loop through of parse objects , add them listview. listview used allow user search through of objects parse.com. objects parse.com should replace listview_array[] data....ex replace "budweiser", "dubra" etc....
the loop did create retrieve objects names, prices, size did work, not place listview since believe object.
here's code
package com.alpha.dealtap; import java.lang.reflect.array; import java.util.list; import java.util.arraylist; import android.app.activity; import android.content.intent; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import com.parse.findcallback; import com.parse.parse; import com.parse.parseexception; import com.parse.parseobject; import com.parse.parsequery; public class search_page extends activity { private listview lv; private edittext et; public string listview_array[] = { "budweiser", "dubra", "corona", "jack daniels", "smirnoff", "keystone light", "natural ice" }; private arraylist<string> array_sort = new arraylist<string>(); public arraylist<object> _arraylist = new arraylist<object>(); int textlength = 0; parseobject dealsobject; int n = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.search_page); parse.initialize(this, "vuz23z6zdil1jbxbvwelpssdu1cltu3yig30ztwy", "4btyoq1qqkows8qvjv7lvu3zoksrrlfyocpzffwj"); // using parsequery...i can grab/locate objects parse // list...here grabbed objects have burnettes , price // of // may need set limit on how many results // loop....so far think limited 100 default parsequery query = new parsequery("deals"); // query.whereequalto("brand", "burnettes"); query.findinbackground(new findcallback() { @override public void done(list<parseobject> objects, parseexception e) { if (e == null) { log.d("brand", "retrieved " + objects.size() + " brands"); (parseobject dealsobject : objects) { // use dealsobject.get('columnname') access // properties of deals object object brands = dealsobject.get("brand").tostring(); _arraylist.add(brands); listview_array = _arraylist; //having trouble putting strings array list....have cast arraylist , causes error! log.d("size", (string) dealsobject.get("size")); log.d("price", (string) dealsobject.get("price")); log.d("brand", (string) dealsobject.get("brand")); n++; } } else { log.d("brand", "error: " + e.getmessage()); } } }); button store = (button) findviewbyid(r.id.b1); button deal = (button) findviewbyid(r.id.b2); lv = (listview) findviewbyid(r.id.listview01); et = (edittext) findviewbyid(r.id.search_box); lv.setadapter(new arrayadapter<object>(this, android.r.layout.simple_list_item_1, listview_array)); et.addtextchangedlistener(new textwatcher() { public void aftertextchanged(editable s) { // abstract method of textwatcher interface. } public void beforetextchanged(charsequence s, int start, int count, int after) { // abstract method of textwatcher interface. } public void ontextchanged(charsequence s, int start, int before, int count) { textlength = et.gettext().length(); array_sort.clear(); (int = 0; < listview_array.length; i++) { if (textlength <= listview_array[i].length()) { if (et.gettext() .tostring() .equalsignorecase( (string) listview_array[i].subsequence( 0, textlength))) { array_sort.add(listview_array[i]); } } } lv.setadapter(new arrayadapter<string>(search_page.this, android.r.layout.simple_list_item_1, array_sort)); } }); store.setonclicklistener(new view.onclicklistener() { // when use onclicklistener...you need onclick method inside // of @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent("com.alpha.dealtap.storepage")); } }); deal.setonclicklistener(new view.onclicklistener() { // when use onclicklistener...you need onclick method inside // of @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent("com.alpha.dealtap.dealpage")); } }); } protected void onlistitemclick(listview l, view v, int position, long id) { // cheese equals position item clicked...so // depends on item clicked string cheese = "tapdeal"; try { class ourclass = class.forname("com.alpha.dealtap." + cheese); intent ourintent = new intent(search_page.this, ourclass); startactivity(ourintent); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpause() { super.onpause(); }
you can use baseadapter class custom listview. here code that.
list<parseobject> parse = new arraylist<parseobject>(); public class customchannellistadapter extends baseadapter { private context context; public customchannellistadapter(context context) { super(); this.context = context; } @override public int getcount() { if (parse != null) { return parse.size(); } return 0; } @override public channel getitem(int position) { // todo auto-generated method stub return parse.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; textview tittle = null; textview desc = null; textview nowplaying = null; if (view == null) { layoutinflater inflater = ((activity) context) .getlayoutinflater(); view = inflater.inflate(r.layout.video_listrow, parent, false); } else { view = convertview; } tittle = (textview) view.findviewbyid(r.id.title); desc = (textview) view.findviewbyid(r.id.descp); nowplaying = (textview) view.findviewbyid(r.id.nowplayingid); if (parse!= null) { if (parse.get(position).getname() != null) { tittle.settext(parse.get(position).getname() .tostring()); } else { tittle.settext("----------------------"); } if (parse.get(position).getdesc() != null) { desc.settext(parse.get(position).getdesc() .tostring()); } else { desc.settext("------------------------"); } } return view; } }
video_listrow.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="400dp" android:layout_height="75dp" android:background="@drawable/videolist_selector" android:orientation="horizontal" android:paddingleft="5dp" android:paddingright="5dp" > <!-- title of song --> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="5dp" android:textcolor="@color/orange" android:textsize="18dip" android:textstyle="bold" android:typeface="sans" android:ellipsize="marquee" android:freezestext="true" android:marqueerepeatlimit="marquee_forever" android:paddingleft="5dip" android:scrollhorizontally="true" android:singleline="true" android:text="tittle not found " /> <!-- artist name --> <textview android:id="@+id/descp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_margintop="1dip" android:layout_torightof="@+id/thumbnail" android:paddingtop="5dp" android:textcolor="@color/white" android:textsize="12dip" android:textstyle="bold" android:typeface="sans" android:ellipsize="marquee" android:freezestext="true" android:marqueerepeatlimit="marquee_forever" android:paddingleft="5dip" android:scrollhorizontally="true" android:singleline="true" android:text="description not found" /> <!-- rightend duration --> <!-- <textview android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_aligntop="@id/title" android:gravity="right" android:text="5:45" android:layout_marginright="5dip" android:textsize="10dip" android:textcolor="#10bcc9" android:textstyle="bold"/> --> <!-- rightend arrow --> <!-- <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow" android:layout_alignparentright="true" android:layout_centervertical="true"/> --> <textview android:id="@+id/nowplayingid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginbottom="5dp" android:layout_marginright="4dp" android:text="now playing..." android:textcolor="@color/red" android:textsize="12dp" /> </relativelayout>
in activity code
listview videolist = (listview) findviewbyid(_); customchannellistadapter channeladapter = new customchannellistadapter(plasmaview.this); videolist.setadapter(channeladapter);
Comments
Post a Comment