Problem Statement
I have tried various methods posted on forum related to this issue but not able to understand what is wrong with my code. Really appreciate any pointers to resolve this
I am working on an application where based on certain selected values a database query is created which fetches relevant rows from database and displays it in list view. Along with text, it also provides name of image file to be displayed along with the results. Image files is stored in drawable folder and loaded along with installation (No online image download). However, on click of button which displays the list view, it displays duplicate records for the data fetched and images.
I am new to android and to community and would like to apologies for putting so much text in query
My Code is 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 + " FROM " + TBL_NM_MSG
+" WHERE "+ TBL_CLMN_GENDER +" = '"+gender+"' AND "
+ TBL_CLMN_RELATION +" LIKE '%"+relation+"%' AND "
+ TBL_CLMN_OCCASION +" LIKE '%"+occasion+"%' AND "
+ TBL_CLMN_AGE +" LIKE '%"+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>