Android - gridview显示重复值(两次)

时间:2014-02-11 06:03:48

标签: android gridview imageview android-arrayadapter

我有一个应用程序,我想用textview和imageview显示gridview ....

这个gridview必须根据点击的按钮更改它的值,即单击abc按钮然后gridview应该只显示该特定的名称和图像然后如果我点击xyz按钮那么只有数据但是现在我正在获取数据两个enter image description here

我已经完成了

mAdapter = new GridviewAdapter(this, listCountry, listFlag);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridview_t);
        gridView.setBackgroundColor(Color.WHITE);

        gridView.setAdapter(mAdapter);

public void get_record()
{
    try {
        db = openOrCreateDatabase("dbname",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);


        cur_BeansLegumesn = db.rawQuery(
                "select name,image from "+ Table_name +"   order by name", null);
        System.out.println("connected this i sdone");

        array_BeansLegumes = new String[cur_BeansLegumesn.getCount()];
        int i = 0, j = 0;
        list = new ArrayList<String>();
        img_list = new ArrayList<String>();
        if (cur_BeansLegumesn.moveToFirst()) {

            do {
                int contentTitleColumn = cur_BeansLegumesn
                        .getColumnIndex("name");
                int img = cur_BeansLegumesn.getColumnIndex("image");
                str = cur_BeansLegumesn.getString(contentTitleColumn)
                        .toString();
                str1 = cur_BeansLegumesn.getString(img).toString();
                // array_BeansLegumes[i]=str;

                // array_BeansLegumes_imgname[i]=str1;
                list.add(str);
                img_list.add(str1);
                System.out.println(str);
//              System.out.println("this is img " + str1);
                i++;

            } while (cur_BeansLegumesn.moveToNext());

        }

        cur_BeansLegumesn.close();

    } catch (Exception e) {
        System.out.println("error " + e + "");
    } finally {

        db.close();

    }

这将填充适配器

public void prepareList() {


        listCountry.addAll(list);

        for (int i = 0; i < list.size(); i++) {

            //System.out.println("this is prepa... " + listCountry.get(i));

        }

        listFlag = new ArrayList<Integer>();

        listimgname = new ArrayList<String>();
        listimgname.removeAll(img_list);
        listimgname.addAll(img_list);
        for (int j = 0; j < img_list.size(); j++) {

            System.out.println("img name " + listimgname.get(j));
            int id = getResources().getIdentifier(listimgname.get(j),
                    "drawable", getPackageName());

            listFlag.add(id);

        }
        mAdapter = new GridviewAdapter(this, listCountry, listFlag);
        gridView = (GridView) findViewById(R.id.gridview_t);
        gridView.setBackgroundColor(Color.WHITE);

        gridView.setAdapter(mAdapter);
    }

这是我的适配器类

public class GridviewAdapter extends BaseAdapter

{
    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private Activity activity;

    public GridviewAdapter(Activity activity, ArrayList<String> listCountry,
            ArrayList<Integer> listFlag) {
        super();

//      this.listCountry = listCountry;
        this.listCountry = new ArrayList<String>();
        this.listCountry=listCountry;
        this.listFlag = listFlag;
        this.activity = activity;
        System.out.println("this is contry name "+this.listCountry);
        System.out.println("this is img  name  "+this.listFlag);
        //System.out.println();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        System.out.println("len " + listCountry.size());
        return listCountry.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.test, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.grid_item_label);

            view.txtViewTitle.setTextColor(Color.BLACK);

            view.imgViewFlag = (ImageView) convertView
                    .findViewById(R.id.grid_item_image);

            view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
        try {
            view.txtViewTitle.setText(listCountry.get(position));
            view.imgViewFlag.setImageResource(listFlag.get(position));

        } catch (Exception e) {
            System.out.println("this is error " + e.getMessage());
        }

        return convertView;
    }
}

数据来自DB,而gree imges表示我点击了xyz btn并且它也必须显示img但是没有显示

这是我的log-cat

02-11 01:02:02.549: I/System.out(4787): this is btn clickedSeafood
02-11 01:02:02.629: W/FileUtils(4787): Failed to chmod(/data/data/com.example.therapeuticfood/databases/Healing_Foods): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted)
02-11 01:02:02.649: I/System.out(4787): connected this i sdone
02-11 01:02:02.659: I/System.out(4787): Cod
02-11 01:02:02.659: I/System.out(4787): Halibut
02-11 01:02:02.659: I/System.out(4787): Salmon
02-11 01:02:02.659: I/System.out(4787): Sardines
02-11 01:02:02.659: I/System.out(4787): Scallops
02-11 01:02:02.659: I/System.out(4787): Shrimp
02-11 01:02:02.659: I/System.out(4787): Tuna
02-11 01:02:02.659: I/System.out(4787): img name cod
02-11 01:02:02.659: I/System.out(4787): img name halibut
02-11 01:02:02.659: I/System.out(4787): img name salmon
02-11 01:02:02.659: I/System.out(4787): img name sardines
02-11 01:02:02.659: I/System.out(4787): img name scallops
02-11 01:02:02.679: I/System.out(4787): img name shrimp
02-11 01:02:02.679: I/System.out(4787): img name tuna
02-11 01:02:02.679: I/System.out(4787): this is contry name [Cod, Halibut, Salmon, Sardines, Scallops, Shrimp, Tuna, Cod, Halibut, Salmon, Sardines, Scallops, Shrimp, Tuna]
02-11 01:02:02.679: I/System.out(4787): this is img  name  [2130837516, 2130837529, 2130837573, 2130837574, 2130837575, 2130837578, 2130837585]
02-11 01:02:02.699: I/System.out(4787): len 14
02-11 01:02:02.699: I/System.out(4787): len 14
02-11 01:02:02.709: I/System.out(4787): len 14
02-11 01:02:02.709: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.719: I/System.out(4787): len 14
02-11 01:02:02.729: I/System.out(4787): len 14
02-11 01:02:02.749: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.769: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.779: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.799: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.809: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.819: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.839: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.839: I/System.out(4787): this is error Invalid index 7, size is 7
02-11 01:02:02.849: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.849: I/System.out(4787): this is error Invalid index 8, size is 7
02-11 01:02:02.869: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.869: I/System.out(4787): this is error Invalid index 9, size is 7
02-11 01:02:02.879: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.879: I/System.out(4787): this is error Invalid index 10, size is 7
02-11 01:02:02.899: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.899: I/System.out(4787): this is error Invalid index 11, size is 7
02-11 01:02:02.909: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.909: I/System.out(4787): this is error Invalid index 12, size is 7
02-11 01:02:02.919: W/Resources(4787): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08000d}
02-11 01:02:02.929: I/System.out(4787): this is error Invalid index 13, size is 7
02-11 01:02:03.009: I/Choreographer(4787): Skipped 34 frames!  The application may be doing too much work on its main thread.

提前致谢

0 个答案:

没有答案