2012-08-26 10 views
6

मेरे पास मेरे एप्लिकेशन में GridView है जिसमें मैं टेक्स्ट दिखाना चाहता हूं और ईमेल इनबॉक्स पेज की तरह बॉक्स देखना चाहता हूं। मैं इसके लिए एक एडाप्टर का उपयोग करता हूं लेकिन जब मैं 15 से अधिक तत्व दिखाता हूं तो शीर्ष पंक्तियों का टेक्स्ट और चेक बॉक्स गायब हो जाता है, इसलिए जब मैं फिर से ऊपर स्क्रॉल करता हूं तो वे दिखाई नहीं देते हैं। यहाँ मेरी कोडस्क्रॉलिंग के दौरान ग्रिड व्यू सामग्री गायब हो जाती है

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

लेआउट एक्सएमएल फ़ाइल

<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

क्या आप एडाप्टर द्वारा उपयोग किए गए एक्सएमएल लेआउट को दिखा सकते हैं? – Vicent

+0

एडाप्टर गतिशील रूप से ऑब्जेक्ट को परिभाषित करता है इसलिए यहां XML लेआउट की आवश्यकता नहीं है। – ADCDER

उत्तर

-1

है मुझे लगता है कि GridView बस भरा हुआ है और स्वचालित रूप से स्क्रॉल (ताकि जब आप नए आइटम जोड़ते हैं तो शीर्ष आइटम 'शीर्ष पर' छोड़ें)। क्या 15 से अधिक वस्तुओं के लिए जगह है? विशेष रूप से जब आपने अपनी ग्रिड व्यू की ऊंचाई 360 डीपी तय की है? आप ग्रिड व्यू को कितनी जगह प्राप्त करते हैं और पहले 15 आइटमों द्वारा कितना स्थान लिया जाता है, यह देखने के लिए आप डिबगिंग के लिए अपने ग्रिड व्यू में पृष्ठभूमि रंग सेट कर सकते हैं।

+0

ग्रिड व्यू की ऊंचाई 360 डीपी पर सेट नहीं है क्योंकि इसमें स्क्रॉलिंग विकल्प है। यह 50 पंक्तियों को भी स्टोर कर सकता है लेकिन जब मैं इसे स्क्रॉल करता हूं तो ऊपरी पंक्तियां अदृश्य हो जाती हैं। – ADCDER

+0

मुझे लगता है कि मैं आपको सही ढंग से समझ नहीं रहा हूं। यदि यह स्क्रॉल करता है, तो हिस्सा अदृश्य हो जाता है। यही वह है जो मैं उम्मीद करता हूं। – Jochem

1

समस्या यह प्रतीत होती है कि आप सही तरीके से रीसाइक्लिंग विचार नहीं कर रहे हैं। निम्नलिखित न्यूनतम उदाहरण आपके कोड के सरलीकरण पर आधारित है (मैंने कुछ सदस्यों और कुछ सशर्त ब्लॉक से छुटकारा पा लिया है)। केवल दो प्रकार की कोशिकाएं, 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; 
    } 
} 

नोट getViewTypeCount और getItemViewType का उपयोग। वे आपकी मदद करते हैं जब आपके getView विभिन्न प्रकार के विचारों से संबंधित होते हैं।

संबंधित मुद्दे