Rawan F. Al-theeb
Rawan F. Al-theeb

Reputation: 1

Grid view item's

I'm using GridView in my application , I set the number of columns to 2 it is working correctly but if the number of items in the grid are greater than 8 items it start repeating the first item.

I want to reach 14 items without repeating any item. Thanks!

Here's my gridview xml

 <GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="160dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:paddingTop="10dp"
    android:layout_below="@id/View1"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

![this is screen shot ,after it reach (cotact 8) it repeat (cotact 1), (cotact 2) ...etc][1] this is my adapter

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

        View v;


        if (convertView == null) {  // if it's not recycled, initialize some attributes

            LayoutInflater li = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            v = li.inflate(R.layout.icon_launcher, null);

            TextView tv = (TextView)v.findViewById(R.id.icon_text); 
            ImageButton ib = (ImageButton)v.findViewById(R.id.icon_image);              
            LinearLayout icon = (LinearLayout)v.findViewById(R.id.icon_launcher);

            //icon.setLayoutParams(new GridView.LayoutParams(203,200));
            ib.setScaleType(ib.getScaleType().FIT_XY);          
            ib.setPadding(1,1,1,1);
            ib.setFocusable(false);
            ib.setClickable(false);          
            ib.setImageBitmap(BitmapFactory.decodeFile(mThumbIds.get(position)));

            tv.setText(mTextsIds.get(position));
 } else

        {
            v = (View) convertView;


        }


        return v;
    }

Upvotes: 0

Views: 280

Answers (2)

faceman
faceman

Reputation: 1328

It's because your Adapter is reusing the views. And in your code you only get the controls when the convertView is null (gets created).


After some items android starts reusing the views (convertView is not null anymore) and your getView method doesn't set the textview etc.


You should set the content (textview etc.) outside your "if (convertView == null)" method like @Glenn suggested.

Upvotes: 0

user948620
user948620

Reputation:

Try this fix.

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



        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes

            LayoutInflater li = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.icon_launcher, null);


        } 

        TextView tv = (TextView) convertView.findViewById(R.id.icon_text);
        ImageButton ib = (ImageButton) convertView.findViewById(R.id.icon_image);
        LinearLayout icon = (LinearLayout) convertView
                        .findViewById(R.id.icon_launcher);

        // icon.setLayoutParams(new GridView.LayoutParams(203,200));
        ib.setScaleType(ib.getScaleType().FIT_XY);
        ib.setPadding(1, 1, 1, 1);
        ib.setFocusable(false);
        ib.setClickable(false);
        ib.setImageBitmap(BitmapFactory.decodeFile(mThumbIds.get(position)));

                tv.setText(mTextsIds.get(position));
        return convertView;
    }

Upvotes: 1

Related Questions