Question

I have 4x25 GridView. Each cells contains RadioButton & TextView.

My adapter:

public class FieldAdapter extends BaseAdapter {
Context context; TextView Cell_num; View Cell;  
LayoutInflater inflater;

  public FieldAdapter(Context context) 
     {
                    this.context=context;
                    inflater = ((Activity) context).getLayoutInflater();                 
     }
  @Override
  public long getItemId(int position) {
    return position;
  }

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

  public int getCount() 
  {
               return 100;
  }

  @Override
     public View getView(int pos, View convertView, ViewGroup parent) 
     {  
      if (convertView == null) 
      {
        Cell = new View (context);
        Cell = inflater.inflate(R.layout.cell, parent, false);
        Cell_num = (TextView)Cell.findViewById(R.id.top_cell_text);
        Cell_num.setText(Integer.toString(pos));
        Log.d("Filled cell number",Integer.toString(pos));

      }
      else {
        Log.d("Else","View already exist");
        Cell = (View) convertView;                   
      }
        return Cell;
     }

}

My Activity class:

public class FieldActivity extends Activity {
    GridView Field; FieldAdapter FieldAdapter; MyMethods Method = new MyMethods();  

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.field);
        Field = (GridView) findViewById(R.id.Field);
        FieldAdapter = new FieldAdapter(this);
        Field.setAdapter(FieldAdapter);
        Method.CreateGrid (Field, this);//here i set numColumns & strechmode
      }//onCreate

}

Once Grid was created, I see 6 rows in my screen with 4 columns. It is 24 elements (0-23)

There is, what I see in LogCat (I add some questions in comments):

05-08 19:07:57.584: D/Filled cell number(19267): 0
05-08 19:07:57.594: I/Adreno200-EGLSUB(19267): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-08 19:07:57.604: D/Else(19267): View already exist /*why else condition was triggered?*/
05-08 19:07:57.614: D/Filled cell number(19267): 1
05-08 19:07:57.614: D/Filled cell number(19267): 2
05-08 19:07:57.624: D/Filled cell number(19267): 3
05-08 19:07:57.624: D/Filled cell number(19267): 4
05-08 19:07:57.634: D/Filled cell number(19267): 5
05-08 19:07:57.634: D/Filled cell number(19267): 6
05-08 19:07:57.644: D/Filled cell number(19267): 7
05-08 19:07:57.644: D/Filled cell number(19267): 8
05-08 19:07:57.644: D/Filled cell number(19267): 9
05-08 19:07:57.654: D/Filled cell number(19267): 10
05-08 19:07:57.664: D/Filled cell number(19267): 11
05-08 19:07:57.674: D/Filled cell number(19267): 12
05-08 19:07:57.684: D/Filled cell number(19267): 13
05-08 19:07:57.684: D/Filled cell number(19267): 14
05-08 19:07:57.694: D/Filled cell number(19267): 15
05-08 19:07:57.694: D/Filled cell number(19267): 16
05-08 19:07:57.704: D/Filled cell number(19267): 17
05-08 19:07:57.704: D/Filled cell number(19267): 18
05-08 19:07:57.704: D/Filled cell number(19267): 19
05-08 19:07:57.714: D/Filled cell number(19267): 20
05-08 19:07:57.714: D/Filled cell number(19267): 21
05-08 19:07:57.724: D/Filled cell number(19267): 22
05-08 19:07:57.724: D/Filled cell number(19267): 23
05-08 19:07:57.734: D/Filled cell number(19267): 0 /*Why again 0 number? Why not 24?*/

Then, if I try to scroll down:

05-08 19:13:15.517: D/Else(19985): View already exist
05-08 19:13:15.527: D/Filled cell number(19985): 25
05-08 19:13:15.537: D/Filled cell number(19985): 26
05-08 19:13:15.537: D/Filled cell number(19985): 27
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.587: D/Else(19985): View already exist
05-08 19:13:15.827: D/Else(19985): View already exist
05-08 19:13:15.827: D/Else(19985): View already exist
... etc

Why creates only 25 26 27 cells? Why cells stop creating and start being duplicated (else condition)

If I try to scrollback my GridView - I see the same picture. I have a mishmash like this:

3  2  1  0
7  6  5  4
8  9  10  11  
...

Need help.

Was it helpful?

Solution

Here is solution of this problem.

Adapter:

ArrayList <View> Cells = new ArrayList <View>();
...
...   
 public void FillArray(ViewGroup parent) {
      while (cellIndex < getCount()){
             Cells.add(inflater.inflate(R.layout.cell,parent, false)); 
             Log.d("ArrayList" + Integer.toString(cellIndex), "is null: " + (Cells.get(cellIndex) == null));
             cellIndex++;
         }   
  }
  public View getView(int pos, View convertView, ViewGroup parent) 
     {  
        FillArray(parent);
        Cell_num = (TextView)Cells.get(pos).findViewById(R.id.top_cell_text);
        Cell_num.setText(Integer.toString(pos));
        Log.d("Filled cell number",Integer.toString(pos));
        Log.d("ArrayList" + Integer.toString(pos), "is null: " + (Cells.get(pos) == null));            
        return Cells.get(pos);
     }

I use ArrayList as a source of View elements for getView method. It makes every cell to remer its state even after scrolling the screen.

However it makes your adapter to refill cell when it cames from the edge of the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top