2010-06-26 19 views
7

पर आइकन जोड़ना मेरे पास गतिशील ListView है जो ArrayAdapter का उपयोग करता है। जब किसी स्पिनर से कोई नाम चुना जाता है, तो एक आइकन के साथ नाम दिखाता है कि वे नर या मादा हैं ListView में जोड़े गए हैं।ArrayAdapter और ViewHolder के साथ ListView गलत आइटम

अधिकतर सबकुछ अच्छा है (नाम आइकन के साथ सही ढंग से सूची में जोड़ा जाता है)। लेकिन लिंग दिखाने वाला आइकन ListView में गलत आइटम में जोड़ा जाता है। नाम सूची के नीचे जोड़ा जाता है, लेकिन आइकन सूची के शीर्ष पर नाम पर रखा जाता है। मुझे नहीं पता कि मैं जिस तरह से ViewHolder का उपयोग कर रहा हूं, लेकिन Android website में शून्य दस्तावेज है।

// Listview inflater 
inflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE); 

// List Array. 
mAdapter = new ArrayAdapter<String>(this, R.layout.player_simple_list, 
               R.id.label, mStrings) { 

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

     Log.i("ANDY","View getView Called"); 
     // A ViewHolder keeps references to children views to 
     // avoid unneccessary calls to findViewById() on each row. 
     ViewHolder holder; 

     if (null == convertView) { 
      Log.i("ANDY","Position not previously used, so inflating"); 
      convertView = inflater.inflate(R.layout.player_simple_list, null); 
      // Creates a ViewHolder and store references to the 
      // two children views we want to bind data to. 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.label); 
      holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
      if (sexmale == true) { 
       holder.icon.setImageBitmap(maleicon); 
      } 
      else { 
       holder.icon.setImageBitmap(femaleicon); 
      } 
      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.text.setText(getItem(position)); 
     // Change icon depending is the sexmale variable is true or false. 
     Log.i("ANDY","getCount = "+mAdapter.getCount()); 
     return convertView; 
    } 
}; 
setListAdapter(mAdapter); 
+0

वहाँ वहाँ कुछ उचित प्रलेखन है: https://developer.android.com/training/improving-layouts/smooth-scrolling.html – Vince

उत्तर

1

अद्यतन:ViewHolder केवल आइटम लेआउट के अंदर घटक विचारों के लिए संदर्भ पकड़ के लिए है। यह findViewById को कई घटकों के साथ जटिल आइटम लेआउट के अंदर प्रत्येक घटक को प्रस्तुत करने के लिए कॉल करने के ओवरहेड से बचने में मदद करता है (TextView, और ImageView इस मामले में)।

मैंने यौन डेटा पुनर्प्राप्त करने और if-else ब्लॉक के बाहर के आइकन सहित सभी दृश्य डेटा सेट करने के लिए इसे नियमित (जिसे getSex कहा जाता है) का उपयोग करके तय किया।

काम कर कोड अब इस तरह दिखता है:

if (null == convertView) { 
    Log.i("ANDY","Position not previously used, so inflating"); 
    convertView = inflater.inflate(R.layout.player_simple_list, null); 

    // Creates a ViewHolder and store references to the two children views 
    // we want to bind data to. 
    holder = new ViewHolder(); 
    holder.text = (TextView) convertView.findViewById(R.id.label); 
    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.text.setText(getItem(position)); 
// Change icon depending is the sexmale variable is true or false. 
if (getSex (getItem(position)) == true) { 
    holder.icon.setImageBitmap(maleicon); 
} 
else { 
    holder.icon.setImageBitmap(femaleicon); 
} 
return convertView; 
1

आप इस question is explained

// Bind the data efficiently with the holder. 

में के रूप में टिप्पणी के बाद यदि डेटा की कुछ लाइनें से स्थानांतरित करने के लिए, तो यह इस

if (null == convertView) { 
    Log.i("ANDY","Position not previously used, so inflating"); 
    convertView = inflater.inflate(R.layout.player_simple_list, null); 
    // Creates a ViewHolder and store references to the two children views 
    // we want to bind data to. 
    holder = new ViewHolder(); 
    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.text = (TextView) convertView.findViewById(R.id.label); 
holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
if (sexmale == true) { 
    holder.icon.setImageBitmap(maleicon); 
} 
else { 
    holder.icon.setImageBitmap(femaleicon); 
} 
holder.text.setText(getItem(position)); 
+3

मैं सहमत नहीं, इससे केवल 'व्यूहोल्डर' बेकार हो जाएगा क्योंकि आप इसे हर बार ओवरराइट कर रहे हैं। सही समाधान को 'if' शाखा में' holder.text' और 'holder.icon' सेट करना चाहिए और' if' ब्लॉक के बाहर सामग्री ('setText', 'setImageBitmap') सेट करना चाहिए। – cristis

+0

मुझे लगता है कि अधिलेखित होने की आवश्यकता है क्योंकि यदि नहीं, तो यह किसी अन्य रिकॉर्ड स्थिति से संबंधित डेटा रखेगा। उदाहरण के लिए आप रिकॉर्ड 10 प्रदर्शित करते हैं, और यदि रिकॉर्ड 3 के लिए डेटा सेट ओवरराइट नहीं करते हैं तो रिकॉर्ड 3 से कैश व्यू का पुन: उपयोग करेगा। – Pentium10

+1

@ पेंटियम 10: नहीं, क्रिस्टिस सही है। 'व्यूहोल्डर' पंक्ति से जुड़ा हुआ है, इसलिए इसमें कौन से विजेट बदल रहे हैं। बस उन विगेट्स की सामग्री को बदलने की जरूरत है। – CommonsWare

2

आप के लिए है की तरह दिखाई देगा है बनाने या बाध्य करने के लिए if-else-if के बाद आइकन सेट करें। अन्यथा, आइकन को केवलभरने तक सूची में पहले कुछ आइटमों में ही प्रदर्शित किया जाएगा।

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

    Log.i("ANDY","View getView Called"); 
    // A ViewHolder keeps references to children views 
    // to avoid unneccessary calls to findViewById() on each row. 
    ViewHolder holder; 

     if (null == convertView) { 
      Log.i("ANDY","Position not previously used, so inflating"); 
      convertView = inflater.inflate(R.layout.player_simple_list, null); 

      // Creates a ViewHolder and store references to 
      // the two children views we want to bind data to. 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.label); 
      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.text.setText(getItem(position)); 

     // Change icon depending is the sexmale variable is true or false. 
     if (sexmale == true) { 
      holder.icon.setImageBitmap(maleicon); 
     } 
     else { 
      holder.icon.setImageBitmap(femaleicon); 
     } 
     Log.i("ANDY","getCount = "+mAdapter.getCount()); 
     return convertView; 
} 
+0

हाय। मैंने कोशिश की, लेकिन सभी आइकन एक ही प्रकार में बदल गए, सभी नीचे सूची। यानी यदि एक पुरुष नाम चुना गया है, तो सभी प्रतीक पुरुष बन जाते हैं। –

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