2014-02-09 25 views
0

的Android griview我有1个应用,需要的图片和文字到网格视图......这样 enter image description here用图片和文字从数据库

,但我得到的是我很两次显示某些数据的问题.. 。我不能够找到问题...... 这是我的logcat

02-09 06:31:41.191: W/FileUtils(4115): Failed to chmod(/data/data/com.javacodegeeks.android.androidgridviewexample/databases/Healing_Foods): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted) 
02-09 06:31:41.231: I/System.out(4115): connected 
02-09 06:31:41.281: I/System.out(4115): Black beans 
02-09 06:31:41.281: I/System.out(4115): Dried peas 
02-09 06:31:41.291: I/System.out(4115): Garbanzo beans 
02-09 06:31:41.291: I/System.out(4115): Kidney beans 
02-09 06:31:41.291: I/System.out(4115): Lentils 
02-09 06:31:41.291: I/System.out(4115): Lima beans 
02-09 06:31:41.291: I/System.out(4115): Miso 
02-09 06:31:41.291: I/System.out(4115): Navy beans 
02-09 06:31:41.291: I/System.out(4115): Pinto beans 
02-09 06:31:41.291: I/System.out(4115): Soybeans 
02-09 06:31:41.291: I/System.out(4115): Tofu And Tempeh 
02-09 06:31:41.481: I/Choreographer(4115): Skipped 65 frames! The application may be doing too much work on its main thread. 
02-09 06:31:41.601: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.601: I/System.out(4115): Black beansthis is mobile data 
02-09 06:31:41.711: D/dalvikvm(4115): GC_FOR_ALLOC freed 78K, 5% free 3128K/3280K, paused 61ms, total 65ms 
02-09 06:31:41.851: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.861: I/System.out(4115): Dried peasthis is mobile data 
02-09 06:31:41.881: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.881: I/System.out(4115): Garbanzo beansthis is mobile data 
02-09 06:31:41.891: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.901: I/System.out(4115): Kidney beansthis is mobile data 
02-09 06:31:41.931: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.931: I/System.out(4115): Lentilsthis is mobile data 
02-09 06:31:41.941: I/Choreographer(4115): Skipped 281 frames! The application may be doing too much work on its main thread. 
02-09 06:31:41.951: W/Resources(4115): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:31:41.961: I/System.out(4115): Black beansthis is mobile data 
02-09 06:31:42.011: D/gralloc_goldfish(4115): Emulator without GPU emulation detected. 
02-09 06:31:48.661: I/Choreographer(4115): Skipped 222 frames! The application may be doing too much work on its main thread. 
02-09 06:31:50.211: D/dalvikvm(4115): GC_FOR_ALLOC freed 20K, 3% free 3551K/3656K, paused 46ms, total 53ms 
02-09 06:31:50.281: I/Choreographer(4115): Skipped 86 frames! The application may be doing too much work on its main thread. 
02-09 06:31:51.411: I/Choreographer(4115): Skipped 133 frames! The application may be doing too much work on its main thread. 
02-09 06:31:52.231: I/Choreographer(4115): Skipped 30 frames! The application may be doing too much work on its main thread. 
02-09 06:31:52.381: I/Choreographer(4115): Skipped 82 frames! The application may be doing too much work on its main thread. 
02-09 06:31:53.171: I/Choreographer(4115): Skipped 59 frames! The application may be doing too much work on its main thread. 
02-09 06:31:53.291: I/Choreographer(4115): Skipped 50 frames! The application may be doing too much work on its main thread. 
02-09 06:31:53.361: I/Choreographer(4115): Skipped 42 frames! The application may be doing too much work on its main thread. 
02-09 06:31:53.571: I/Choreographer(4115): Skipped 39 frames! The application may be doing too much work on its main thread. 
02-09 06:31:54.111: I/Choreographer(4115): Skipped 34 frames! The application may be doing too much work on its main thread. 
02-09 06:31:54.251: I/Choreographer(4115): Skipped 34 frames! The application may be doing too much work on its main thread. 
02-09 06:32:35.661: W/FileUtils(4361): Failed to chmod(/data/data/com.javacodegeeks.android.androidgridviewexample/databases/Healing_Foods): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted) 
02-09 06:32:35.711: I/System.out(4361): connected 
02-09 06:32:35.831: I/System.out(4361): Black beans 
02-09 06:32:35.831: I/System.out(4361): Dried peas 
02-09 06:32:35.831: I/System.out(4361): Garbanzo beans 
02-09 06:32:35.841: I/System.out(4361): Kidney beans 
02-09 06:32:35.841: I/System.out(4361): Lentils 
02-09 06:32:35.841: I/System.out(4361): Lima beans 
02-09 06:32:35.841: I/System.out(4361): Miso 
02-09 06:32:35.841: I/System.out(4361): Navy beans 
02-09 06:32:35.841: I/System.out(4361): Pinto beans 
02-09 06:32:35.841: I/System.out(4361): Soybeans 
02-09 06:32:35.841: I/System.out(4361): Tofu And Tempeh 
02-09 06:32:36.231: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:36.261: I/System.out(4361): Black beansthis is mobile data 
02-09 06:32:36.461: D/dalvikvm(4361): GC_FOR_ALLOC freed 82K, 5% free 3128K/3280K, paused 118ms, total 133ms 
02-09 06:32:36.811: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:36.811: I/System.out(4361): Dried peasthis is mobile data 
02-09 06:32:36.931: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:36.931: I/System.out(4361): Garbanzo beansthis is mobile data 
02-09 06:32:37.001: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:37.001: I/System.out(4361): Kidney beansthis is mobile data 
02-09 06:32:37.041: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:37.041: I/System.out(4361): Lentilsthis is mobile data 
02-09 06:32:37.061: I/Choreographer(4361): Skipped 647 frames! The application may be doing too much work on its main thread. 
02-09 06:32:37.081: W/Resources(4361): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:32:37.081: I/System.out(4361): Black beansthis is mobile data 
02-09 06:32:37.161: D/gralloc_goldfish(4361): Emulator without GPU emulation detected. 
02-09 06:32:37.901: I/Choreographer(4361): Skipped 330 frames! The application may be doing too much work on its main thread. 
02-09 06:32:38.331: I/Choreographer(4361): Skipped 39 frames! The application may be doing too much work on its main thread. 
02-09 06:33:20.161: I/Choreographer(4361): Skipped 71 frames! The application may be doing too much work on its main thread. 
02-09 06:33:20.731: I/Choreographer(4361): Skipped 216 frames! The application may be doing too much work on its main thread. 
02-09 06:33:22.081: D/dalvikvm(4361): GC_FOR_ALLOC freed 21K, 3% free 3551K/3656K, paused 64ms, total 68ms 
02-09 06:33:22.111: I/Choreographer(4361): Skipped 169 frames! The application may be doing too much work on its main thread. 
02-09 06:33:22.461: I/Choreographer(4361): Skipped 32 frames! The application may be doing too much work on its main thread. 
02-09 06:33:22.931: I/Choreographer(4361): Skipped 50 frames! The application may be doing too much work on its main thread. 
02-09 06:33:23.131: I/Choreographer(4361): Skipped 78 frames! The application may be doing too much work on its main thread. 
02-09 06:33:23.241: I/Choreographer(4361): Skipped 67 frames! The application may be doing too much work on its main thread. 
02-09 06:33:23.491: I/Choreographer(4361): Skipped 51 frames! The application may be doing too much work on its main thread. 
02-09 06:33:23.611: I/Choreographer(4361): Skipped 32 frames! The application may be doing too much work on its main thread. 
02-09 06:33:24.261: I/Choreographer(4361): Skipped 39 frames! The application may be doing too much work on its main thread. 
02-09 06:33:25.031: I/Choreographer(4361): Skipped 31 frames! The application may be doing too much work on its main thread. 
02-09 06:33:27.861: I/Choreographer(4361): Skipped 38 frames! The application may be doing too much work on its main thread. 
02-09 06:33:28.521: I/Choreographer(4361): Skipped 52 frames! The application may be doing too much work on its main thread. 
02-09 06:33:28.621: I/Choreographer(4361): Skipped 53 frames! The application may be doing too much work on its main thread. 
02-09 06:33:28.721: I/Choreographer(4361): Skipped 31 frames! The application may be doing too much work on its main thread. 
02-09 06:33:28.831: I/Choreographer(4361): Skipped 35 frames! The application may be doing too much work on its main thread. 
02-09 06:33:28.981: I/Choreographer(4361): Skipped 38 frames! The application may be doing too much work on its main thread. 
02-09 06:33:29.421: I/Choreographer(4361): Skipped 39 frames! The application may be doing too much work on its main thread. 
02-09 06:33:29.591: I/Choreographer(4361): Skipped 52 frames! The application may be doing too much work on its main thread. 
02-09 06:33:30.161: I/Choreographer(4361): Skipped 64 frames! The application may be doing too much work on its main thread. 
02-09 06:33:30.301: I/Choreographer(4361): Skipped 52 frames! The application may be doing too much work on its main thread. 
02-09 06:35:00.891: W/FileUtils(4986): Failed to chmod(/data/data/com.javacodegeeks.android.androidgridviewexample/databases/Healing_Foods): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted) 
02-09 06:35:00.941: I/System.out(4986): connected 
02-09 06:35:01.051: I/System.out(4986): Black beans 
02-09 06:35:01.051: I/System.out(4986): Dried peas 
02-09 06:35:01.061: I/System.out(4986): Garbanzo beans 
02-09 06:35:01.061: I/System.out(4986): Kidney beans 
02-09 06:35:01.061: I/System.out(4986): Lentils 
02-09 06:35:01.061: I/System.out(4986): Lima beans 
02-09 06:35:01.061: I/System.out(4986): Miso 
02-09 06:35:01.061: I/System.out(4986): Navy beans 
02-09 06:35:01.061: I/System.out(4986): Pinto beans 
02-09 06:35:01.061: I/System.out(4986): Soybeans 
02-09 06:35:01.061: I/System.out(4986): Tofu And Tempeh 
02-09 06:35:01.091: I/System.out(4986): 11 
02-09 06:35:01.091: I/System.out(4986): 11 
02-09 06:35:01.461: I/System.out(4986): 11 
02-09 06:35:01.591: D/dalvikvm(4986): GC_FOR_ALLOC freed 84K, 5% free 3126K/3280K, paused 72ms, total 83ms 
02-09 06:35:01.591: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:01.631: I/System.out(4986): Black beansthis is mobile data 
02-09 06:35:01.961: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:01.981: I/System.out(4986): Dried peasthis is mobile data 
02-09 06:35:02.001: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:02.031: I/System.out(4986): Garbanzo beansthis is mobile data 
02-09 06:35:02.111: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:02.111: I/System.out(4986): Kidney beansthis is mobile data 
02-09 06:35:02.221: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:02.221: I/System.out(4986): Lentilsthis is mobile data 
02-09 06:35:02.231: I/Choreographer(4986): Skipped 570 frames! The application may be doing too much work on its main thread. 
02-09 06:35:02.371: I/System.out(4986): 11 
02-09 06:35:02.391: W/Resources(4986): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070001} 
02-09 06:35:02.391: I/System.out(4986): Black beansthis is mobile data 
02-09 06:35:02.461: D/gralloc_goldfish(4986): Emulator without GPU emulation detected. 
02-09 06:35:02.801: I/Choreographer(4986): Skipped 68 frames! The application may be doing too much work on its main thread. 
02-09 06:35:02.811: I/System.out(4986): 11 
02-09 06:38:00.921: I/System.out(4986): 11 
02-09 06:38:01.211: I/Choreographer(4986): Skipped 183 frames! The application may be doing too much work on its main thread. 

,代码:

package com.javacodegeeks.android.androidgridviewexample; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MyAdapter extends BaseAdapter { 
    private Context context; 
    private final String[] countries; 

    public MyAdapter(Context context, String[] countries) { 
     this.context = context; 
     this.countries = countries; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View gridView; 

     if (convertView == null) { 

      gridView = new View(context); 

      gridView = inflater.inflate(R.layout.countries, null); 

      TextView textView = (TextView) gridView.findViewById(R.id.label); 

      textView.setText(countries[position]); 

      ImageView flag = (ImageView) gridView .findViewById(R.id.flag); 

      String mobile = countries[position]; 
System.out.println(mobile+"this is mobile data "); 
      if (mobile.equals("abc")) { 
       flag.setImageResource(R.drawable.greekflag); 
      } else if (mobile.equals("xyz")) { 
       flag.setImageResource(R.drawable.germanflag); 
      } else if (mobile.equals("ab")) { 
       flag.setImageResource(R.drawable.italianflag); 
      } 


      else { 
       flag.setImageResource(R.drawable.britishflag); 
      } 




     } else { 
      gridView = (View) convertView; 
     } 

     return gridView; 
    } 

    @Override 
    public int getCount() { 
     System.out.println(countries.length); 
     return countries.length; 
    } 


@Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

} 

回答

0

在你getView方法您只在第一次创建视图时设置文本和图像 - convertView为空时。

您需要创建一个ViewHolder类,并使用setTag将其附加到视图。然后,您可以在创建视图和重新使用视图时设置文本和图像。

使用ViewHolder模式的一个很好的例子是here