从数据库中获取数据时Listview中的重复值

时间:2013-07-18 14:39:39

标签: android android-listview

问题陈述

我已尝试在论坛上发布与此问题相关的各种方法,但无法理解我的代码有什么问题。真的很感激任何指针来解决这个问题

我正在开发一个应用程序,在该应用程序中,基于某些选定值创建数据库查询,该数据库查询从数据库中获取相关行并在列表视图中显示它。除文本外,它还提供要与结果一起显示的图像文件的名称。图像文件存储在可绘制文件夹中,并随安装一起加载(无在线图像下载)。但是,在单击显示列表视图的按钮时,它会显示所获取数据和图像的重复记录。

我是Android和社区的新手,并且想在查询中添加如此多的文本而道歉

我的代码粘贴在

下面

活性

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适配器

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文件

<?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>

0 个答案:

没有答案