gridview元素的重复......!

Repetition of gridview elements.....!

代码可以正常工作,但问题是,某些元素在文本视图中重复出现,而另一些则被遗漏了。例如 "Who we are" 显示两次。我也无法将 textview 元素居中。我已经发布了它的屏幕截图。我是 android 编程的新手,任何人都可以帮助我。

这是主要的Activity

public class MainActivity extends Activity {

    GridView grid;
    public WebView webView;
    public int pos;

    String[] desc = { "Who We Are", "What We Do", "Entrepreneur", "Scholarship", "Admission",
            "Internship", "Industrial Visit", "Project", "Buy or Sell Projects", "Free Training", "College Registration",
            "Information", "Feedback", "Contact"    };


    int[] imageId = { R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
            R.drawable.dummy, R.drawable.dummy 

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);

        GridDesign adapter = new GridDesign(MainActivity.this, desc, imageId);
        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                pos = position;
                Intent intent = new Intent(MainActivity.this, webView.class);
                startActivity(intent);
            }
        });

    }

}

这是自定义网格视图class。

public class GridDesign extends BaseAdapter{
    private Context mContext;
    private final String[] desc;
    private final int[] Imageid;

    public GridDesign(Context c, String[] web, int[] Imageid) {
        mContext = c;
        this.Imageid = Imageid;
        this.desc = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return desc.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_element, parent, false);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView) grid
                    .findViewById(R.id.grid_image);
            textView.setText(desc[position]);
            imageView.setImageResource(Imageid[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

这是主要的activity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dip"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="spacingWidthUniform" />

</LinearLayout>

这是单个网格元素布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/im" >
    </ImageView>

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLength="23"
        android:gravity="center"
        android:layout_marginTop="10sp"
        android:textSize="12sp" >
    </TextView>

</LinearLayout>

如果 convertView 不是 null,您的 getView 方法不会重置您的 View 的内容。如果 convertView 不是 null,您需要设置内容,否则它将保留旧视图中的旧内容,并且您会看到重复的数据。如果 convertView 不是 null,则意味着您正在重用已滚出屏幕的视图(这使滚动更顺畅,因为您不必在滚动时重新分配和取消分配视图。因此,正是这种重用意味着您会看到一些元素的重复和一些元素的丢失,因为在您的 getView 方法中,您没有在重用视图时重置内容。

要解决此问题,您可以将设置视图内容的代码移到 if 语句之外,这样您就可以执行以下操作:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_element, parent, false);
    } else {
        grid = (View) convertView;
    }

    TextView textView = (TextView) grid.findViewById(R.id.grid_text);
    ImageView imageView = (ImageView) grid
            .findViewById(R.id.grid_image);
    textView.setText(desc[position]);
    imageView.setImageResource(Imageid[position]);
    return grid;
}

问题是由回收视图的重用引起的,您可能正在获取之前已经初始化的视图。可以每次都初始化视图,重写代码如下:

        if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_element, parent, false);
        } else {
        grid = (View) convertView;
        }

        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView) grid
                .findViewById(R.id.grid_image);
        textView.setText(desc[position]);
        imageView.setImageResource(Imageid[position]);

或者更好的是,采用 ViewHolder 模式 (View Holder Pattern)。