android - Repeated values in Listview when data fetched from database -


problem statement

i have tried various methods posted on forum related issue not able understand wrong code. appreciate pointers resolve this

i working on application based on selected values database query created fetches relevant rows database , displays in list view. along text, provides name of image file displayed along results. image files stored in drawable folder , loaded along installation (no online image download). however, on click of button displays list view, displays duplicate records data fetched , images.

i new android , community , apologies putting text in query

my code pasted below

activity

import java.util.arraylist; import java.util.list; import android.app.activity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview;  public class giftresult extends activity{     bundle bundle;     listview lv;     list<rowitem> rowitems;     databasehelper dbhelp;     listadapter listadapter;     public void oncreate(bundle savedinstancestate){         dbhelp = new databasehelper(this);         dbhelp.addgift();         super.oncreate(savedinstancestate);         setcontentview(r.layout.giftresult);         rowitems = new arraylist<rowitem>();         listview lv = (listview)findviewbyid(r.id.giftlist);         arraylist<rowitem> item = getdata();         listadapter = new listadapter(this,r.layout.list_row,item);         lv.setadapter(listadapter);         listadapter.notifydatasetchanged();          } public arraylist<rowitem> getdata(){     sqlitedatabase db = dbhelp.getreadabledatabase();     bundle=this.getintent().getextras();     string relation = bundle.getstring("relation");     string gender = bundle.getstring("gender");     string occasion = bundle.getstring("occasion");     string age = bundle.getstring("age");     string tbl_nm_msg = databasehelper.table_name;     string tbl_clmn_image = databasehelper.column_images;     string tbl_clmn_gft = databasehelper.column_gift;     string tbl_clmn_gftdesc = databasehelper.column_giftdesc;     string tbl_clmn_gender = databasehelper.column_gender;     string tbl_clmn_relation = databasehelper.column_relation;     string tbl_clmn_occasion= databasehelper.column_occasion;     string tbl_clmn_age = databasehelper.column_age;     arraylist<rowitem> result = new arraylist<rowitem>();     result.clear();     cursor cur = db.rawquery("select " + tbl_clmn_image +" ,"+ tbl_clmn_gft +" ,"+ tbl_clmn_gftdesc + " " + tbl_nm_msg              +" "+ tbl_clmn_gender +" = '"+gender+"' , "             + tbl_clmn_relation +" '%"+relation+"%' , "              + tbl_clmn_occasion +" '%"+occasion+"%' , "             + tbl_clmn_age +" '%"+age+"%';", null);     cur.movetofirst();     while(cur.isafterlast()== false){         string gift = cur.getstring(cur.getcolumnindex(tbl_clmn_gft));         string giftdesc = cur.getstring(cur.getcolumnindex(tbl_clmn_gftdesc));         string image = cur.getstring(cur.getcolumnindex(tbl_clmn_image));         int imageid = getresources().getidentifier(image, "drawable", this.getpackagename());         rowitem item = new rowitem(imageid, gift, giftdesc) ;         if(result.contains(item)){             result.remove(item);         }         else         result.add(item);         cur.movetonext();     }     cur.close();     return result; } } 

custome adapter

import java.util.list; import android.app.activity; import android.content.context; import android.graphics.drawable.drawable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview;  public class listadapter extends arrayadapter<rowitem> {     context context;     public listadapter(context context, int resourceid,             list<rowitem> items) {         super(context, resourceid, items);         // todo auto-generated constructor stub         this.context=context;     }       private class viewholder {             imageview imageview;             textview txttitle;             textview txtdesc;      }       public view getview(int position, view convertview,viewgroup parent){         viewholder holder = null;          rowitem rowitem = getitem(position);         layoutinflater minflater = (layoutinflater) context                 .getsystemservice(activity.layout_inflater_service);         if (convertview == null) {             convertview = minflater.inflate(r.layout.list_row, null);             holder = new viewholder();             holder.txtdesc = (textview) convertview.findviewbyid(r.id.desc);             holder.txttitle = (textview) convertview.findviewbyid(r.id.title);             holder.imageview = (imageview) convertview.findviewbyid(r.id.list_image);             convertview.settag(holder);         } else             {            holder = (viewholder) convertview.gettag();             }            holder.txtdesc.settext(rowitem.getdesc());            holder.txttitle.settext(rowitem.gettitle());            holder.imageview.setimageresource(rowitem.getimageid());         return convertview;      }      } 

xml file

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"      android:orientation="horizontal"     android:padding="5dip"     android:background="@drawable/list_selector">     <!--  listrow left sied thumbnail image -->     <linearlayout android:id="@+id/thumbnail"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:padding="3dip"         android:layout_alignparentleft="true"         android:layout_marginright="5dip"         android:background="@drawable/image_bg">          <imageview             android:id="@+id/list_image"             android:layout_width="100dip"             android:layout_height="100dip"/>      </linearlayout>      <!-- title of song-->     <textview         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_aligntop="@+id/thumbnail"         android:layout_torightof="@+id/thumbnail"         android:textcolor="#040404"         android:typeface="sans"         android:textsize="20dip"         android:textstyle="bold"/>      <!-- description -->     <textview         android:id="@+id/desc"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_below="@id/title"         android:textcolor="#343434"         android:textsize="16dip"         android:layout_margintop="1dip"         android:layout_torightof="@+id/thumbnail"/>       <!--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="more.."         android:layout_marginright="5dip"         android:textsize="18dip"         android:textcolor="#10bcc9"         android:textstyle="bold"/>       </relativelayout> 


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -