重复gridview元素.....!

时间:2015-07-12 15:49:33

标签: android android-linearlayout textview android-gridview

代码正在运行,但问题在于,某些元素在文本视图中重复,有些元素被忽略了。例如"我们是谁"显示两次。此外,我无法将textview元素置于中心位置。我已经发布了它的截图。我是android编程的新手,所以帮助我,任何人。

这是主要活动

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);
            }
        });

    }

}

这是自定义网格视图类。

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;
    }
}

这是主要的活动布局

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

2 个答案:

答案 0 :(得分:1)

如果getView不是View,则convertView方法不会重置null的内容。如果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;
}

答案 1 :(得分:0)

问题是由重复使用循环视图引起的,您可能会从之前获得已经初始化的视图。您可以每次初始化视图,重写代码如下:

        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)。