2010-11-10 14 views
11

मैं एक गतिशील arrayadapter.it कार्यों से प्रदर्शित करने के लिए व्यूल्डर का उपयोग कर रहा हूं लेकिन जब मैं सूची स्क्रॉल करता हूं तो डेटा अनियमित रूप से प्रदर्शित होता है। मैं चाहता हूं कि मेरी सूची दृश्य केवल एक बार आबादी हो, हर बार जब मैं अपनी सूची स्क्रॉल करता हूं। कोई सुझाव? यहाँ मेरी कोडव्यू धारक को कैसे कार्यान्वित करें?

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

    // A ViewHolder keeps references to children views to avoid unneccessary calls    
    // to findViewById() on each row.    
    ViewHolder holder;    
    // When convertView is not null, we can reuse it directly, there is no need    
    // to reinflate it. We only inflate a new View when the convertView supplied    
    // by ListView is null.    

    if (convertView == null) {     

    convertView = mInflater.inflate(R.layout.sample, null); 
    // Creates a ViewHolder and store references to the two children views     
    // we want to bind data to.    
    holder = new ViewHolder();     
    holder.name = (TextView) convertView.findViewById(R.id.text);    
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);     
    convertView.setTag(holder);    
    } else {     
    // Get the ViewHolder back to get fast access to the TextView     
    // and the ImageView. 


    holder = (ViewHolder) convertView.getTag(); 

    }    


    // Bind the data efficiently with the holder. 
    if(_first==true) 
    { 
    if(id<myElements.size()) 
    { 
     holder.name.setText(myElements.get(id)); 
    holder.icon.setImageBitmap(mIcon1); 
    id++; 
    } 
    else 
    { 
    _first=false; 
    } 
    } 
    //holder.icon.setImageBitmap(mIcon2); 
    /*try{ 
    if(id<myElements.size()) 
    id++; 
    else 
    { 
     id--; 
    } 
    } 
    catch(Exception e) 
    { 
    android.util.Log.i("callRestService",e.getMessage()); 
    }*/ 



    return convertView; 

    }   
static class ViewHolder {    
TextView name;    
ImageView icon;   
} 

है जब सूची यह इस तरह दिखता है भरी हुई है: कुछ डेटा http://i.stack.imgur.com/sMbAD.pngalt text यह इस तरह दिखता है स्क्रॉल के बाद http://i.stack.imgur.com/NrGhR.pngalt text, और फिर अगर मैं शुरुआत करने के लिए स्क्रॉल यह http://i.stack.imgur.com/0KjMa.pngalt text

लग रहा है

पीएस: मेरी सूची को वर्णानुक्रम में होना चाहिए

+0

क्या आप अपने आउटपुट के स्क्रीन शॉट पोस्ट कर सकते हैं? –

+0

@ टिलसन द फाइटर: स्नैपशॉट्स पोस्ट कर चुके हैं। – Ads

उत्तर

5

ऐसा करने के लिए आपको ListView का अपना संस्करण लिखना होगा (जो खराब है)। यदि ListView ठीक से काम नहीं करता है, तो शायद इसका मतलब है कि आप कुछ गलत कर रहे हैं।

id तत्व कहां से आता है? आपको अपनी getView() विधि में स्थिति मिल रही है, इसलिए आपको सूची सीमाओं से अधिक चिंता करने की आवश्यकता नहीं है। स्थिति अपनी सूची में तत्व स्थिति से जुड़ा हुआ है, तो आप इस तरह सही तत्व प्राप्त कर सकते हैं:

myElements.get(position); 

अपनी सूची में परिवर्तन में डेटा, तो आप इस कॉल कर सकते हैं:

yourAdapter.notifyDataSetChanged() 

कि नए डेटा के साथ आपकी सूची का पुनर्निर्माण करेगा (आपकी स्क्रॉलिंग और सामान को रखते हुए)।

27

क्या आपने यह कोशिश की है?

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

    // A ViewHolder keeps references to children views to avoid unneccessary calls    
    // to findViewById() on each row.    
    ViewHolder holder;    
    // When convertView is not null, we can reuse it directly, there is no need    
    // to reinflate it. We only inflate a new View when the convertView supplied    
    // by ListView is null.    

    if (convertView == null) {     

    convertView = mInflater.inflate(R.layout.sample, null); 
    // Creates a ViewHolder and store references to the two children views     
    // we want to bind data to.    
    holder = new ViewHolder();     
    holder.name = (TextView) convertView.findViewById(R.id.text);    
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);     
    convertView.setTag(holder);    
    } else {     
    // Get the ViewHolder back to get fast access to the TextView     
    // and the ImageView. 


    holder = (ViewHolder) convertView.getTag(); 

    }    


    // Bind the data efficiently with the holder. 
    holder.name.setText(myElements.get(id)); 
    holder.icon.setImageBitmap(mIcon1); 

    return convertView; 
    } 

static class ViewHolder {    
    TextView name;    
    ImageView icon;   
} 

यदि हां, तो इसमें क्या गलत है?

मुझे नहीं लगता कि सभी पंक्तियों को एक साथ लोड करना एक अच्छा विचार है। आप स्मृति में बहुत सारे बेकार दृश्यों को समाप्त कर देंगे जो कुछ भी नहीं के लिए एप्लिकेशन को धीमा करने जा रहे हैं। विचारों पर दृश्य और संचालन (जैसे inflate, findViewById, getChild ..) महंगे हैं, आपको जितना संभव हो उतना पुन: उपयोग करने का प्रयास करना चाहिए। यही कारण है कि हम ViewHolders का उपयोग करते हैं।

+0

हाँ! मैंने कोशिश की। जब मैं एलिमेंट (> 9 2) तक स्क्रॉल करता हूं तो यह सूचीदृश्य में तत्व लोड नहीं कर सका! myElement आईडी की सहायता से एक सरणी सूची है, मुझे टेक्स्ट तत्व में प्रदर्शित करने के लिए हर तत्व मिलता है। हां ऐसा लगता है कि मैं एक बार सभी डेटा लोड कर रहा हूं लेकिन मुझे नहीं पता कि इसका पुन: उपयोग कैसे किया जाए, क्या आप कृपया इसमें मदद कर सकते हैं – Ads

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