2012-08-26 28 views
6

私のアプリケーションでは、メールボックスのページと同じようにテキストとチェックボックスを表示したいのですが、GridViewがあります。私はそれを使用してアダプタを使用しますが、15以上の要素を表示すると、一番上の行のテキストボックスとチェックボックスが消えるので、もう一度上にスクロールすると表示されません。ここでは、クラスをアダプタにスクロール中にGridViewのコンテンツが消える

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

コールのように私のコードです:

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

レイアウトXMLファイルが

<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> 
+0

アダプタで使用されているxmlレイアウトを表示できますか? – Vicent

+0

アダプターはオブジェクトを動的に定義するので、XMLレイアウトは不要です。 – ADCDER

答えて

-1

である私は、GridViewコントロールがちょうど一杯になると、自動的に(そうスクロールダウンだと思います新しいアイテムを追加すると、最初のアイテムが最初に '離れる')。 15項目以上のスペースがありますか?特に、GridViewの高さを360dpに固定してからですか?デバッグのためにGridViewに背景色を設定して、GridViewがどのくらいのスペースを取得し、最初の15項目でどれくらいの領域を占めるかを確認できます。

+0

GridViewの高さを360dpに設定しても、スクロールオプションがあるため問題ありません。それは50行も格納できますが、上の行はスクロールすると見えなくなります。 – ADCDER

+0

私はあなたを正しく理解していないと思います。スクロールすると、一部が見えなくなります。それは私が期待するものです。 – Jochem

1

問題は、ビューを適切にリサイクルしていないように見えます。以下の最小限の例は、コードの簡素化に基づいています(私はいくつかのメンバーといくつかの条件付きブロックを取り除いています)。 2種類の細胞、TextViewおよびCheckBoxのみが考慮される。

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

getViewTypeCountgetItemViewTypeの使用に注意してください。 getViewがさまざまな種類のビューを扱うときに役立ちます。

関連する問題