6

I have a GridView in my application in which I want to show text and check boxes just like emails inbox page. I use an adapter for that but when I show more than 15 elements the text and check boxes of top rows disappear so when I scroll upside again they aren't visible. Here is my code

public class EmployeeAdaptor extends BaseAdapter {  
Context context;
String [] table;
int pos;
int formatOfTable;
public EmployeeAdaptor(Context c,String []tab,int numberOfItems,int format, boolean[] sel) {
    context = c;
    table = tab;
    items = numberOfItems;
    formatOfTable = format;
    //ifformat is 0 then show text view in first column
    //if it is 1 then show radio button in first column
    //if it is 2 then show check box in first column.
    pos = 0;
}
public int getCount() {
    // 
    return items;
}

public Object getItem(int position) {
    // 
    return position;
}

public long getItemId(int position) {
    // 
    return position;
}

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

    View v;
    TextView text = new TextView(context);
    RadioButton radio = new RadioButton(context);
    CheckBox check = new CheckBox(context);

    if(convertView == null){
        v = new View(context);
    }
    else{
        v = convertView;
    }

    if(formatOfTable==2 && position%5==0 && position/5!=0){
        check.setId(position/5);
        v = check;

    }
    else if(formatOfTable==0 && position%5==0 && position/5!=0){
        text.setText("");
        v = text;
        //To set blank text at first position when no need of check
    }
    else{
        if(position%5!=0){
            try{
                v = text;
                text.setText(table[pos]);
                text.setTextColor(Color.BLACK);
                text.setTextSize(20);
                pos++;
            }
            catch(Exception e){

            }
        }
    }
    if(position/5==0){
        text.setTypeface(Typeface.DEFAULT_BOLD);
    }
    return v;
}
  }

Call to adapter class is as:

table.setAdapter(new EmployeeAdaptor(this, empTable, numberofboxes, 0, selected));
//OR
table.setAdapter(new EmployeeAdaptor(this, empTable, numberofboxes, 1, selected));

Layout XML file is

<GridView
    android:id="@+id/gridView1"
    android:layout_width="wrap_content"
    android:layout_height="360dp"
    android:layout_marginTop="40dp"
    android:numColumns="5" android:verticalSpacing="35dp">
</GridView>
j0k
  • 22,600
  • 28
  • 79
  • 90
ADCDER
  • 61
  • 1
  • 4

3 Answers3

1

The problem seems to be that you aren't recycling views properly. The following minimal example is based on a simplification of your code (I've got rid off some members and some conditional blocks). Only two kinds of cells, TextView and CheckBox are considered.

public class EmployeeAdaptor extends BaseAdapter {  
    private Context context;
    public String [] table;
    private int items;

    public EmployeeAdaptor(Context c,String []tab,int numberOfItems) {
        context = c;
        items = numberOfItems;
    }

    @Override
    public int getCount() {return items;}

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

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

    @Override
    public int getItemViewType(int position) {
        int viewType;
        if (position%5 == 0 && position/5 != 0) {
            // Positions 5, 10, 15...
            // Return a CheckBox
            viewType = 0;
        } else {
            // Return a TextView
            viewType = 1;
        }
        return viewType;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            if (position%5 == 0 && position/5 != 0) {
                // Positions 5, 10, 15...
                // Return a CheckBox
                v = new CheckBox(context);
            } else {
                // Return a TextView
                v = new TextView(context);
            }
        } else {
            v = convertView;
        }
        if (position < 5) {
            ((TextView)v).setTypeface(Typeface.DEFAULT_BOLD);
            ((TextView)v).setText(table[position]);
        } else if (position%5 == 0 && position/5 != 0) {
            // Positions 5, 10, 15...
            ((CheckBox)v).setId(position/5);
        } else {
            ((TextView)v).setTypeface(Typeface.DEFAULT);
            ((TextView)v).setText(table[position]);
        }
        return v;
    }
}

Note the use of getViewTypeCount and getItemViewType. They help you when your getView deals with different types of views.

Vicent
  • 5,322
  • 2
  • 28
  • 36
0

You need to give the reasonable length of text to be displayed lets say for Textview add this attribute android:maxLength="5". keep on adjusting the length until it nolonger disappears

-1

I think the GridView is just full and automatically scrolls down (so the first items 'leave' at the top) when you add new items. Is there space for more than 15 items? Especially since you fixed the height of your GridView to 360dp? You could set a background color to your GridView for debugging to see how much space the GridView gets and how much is taken up by the first 15 items.

Jochem
  • 2,995
  • 16
  • 18
  • The GridView's height set to 360dp don't matters because it has scrolling option. It can store 50 rows also but upper rows become invisible when I scrolls it. – ADCDER Aug 26 '12 at 13:02
  • I think I am not understanding you correctly. If it scrolls, part becomes invisible. That is what I would expect. – Jochem Aug 26 '12 at 20:33