0

У меня есть пользовательский адаптер для ListView
метод GetView что-то вроде этого:получает неправильную деталь в пользовательских ListView адаптере

public View getView(int position, View convertView, ViewGroup parent) 
    { 
     //final ILSpeechBubble speechBubble; 

     if(convertView == null) 
     { 
      inflater = context.getLayoutInflater(); 
      if(m_msg.get(position).Recieved == 0) 
      { 
       convertView = inflater.inflate(R.layout.text_message_sent_row_template, null); 

       holder = new ViewHolder(); 
       holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1); 

       holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic); 
       holder.imgSend = (ImageView) convertView.findViewById(R.id.imgMessageSent); 

       holder.imgDeliverd = (ImageView) convertView.findViewById(R.id.imgMessageDeliverd); 
       holder.pbSending = (ProgressBar) convertView.findViewById(R.id.pbSendingMessage); 

       holder.spBubble.isChache = false; 
       holder.Position = position; 
       convertView.setTag(holder); 
       m_ViewHolder.add(holder); 
      } 
      else 
      { 
       convertView = inflater.inflate(R.layout.text_message_recieved_row_template, null); 

       holder = new ViewHolder(); 
       holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1); 
       holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic); 
       holder.spBubble.isChache = false; 
       holder.Position = position; 
       convertView.setTag(holder); 
       m_ViewHolder.add(holder); 
      } 
     } 
     else 
     { 
      holder = (ViewHolder) convertView.getTag(); 
      if(holder.Position == position) 
       Log.i("View Holder","Position : " + position); 
      Log.i("View Holder","Position : " + position + " Wrong"); 
      Log.i("View Holder","Position : " + holder.Position + " HolderPos"); 
      //position = holder.Position; 
      holder = findHolder(position); 
      if(holder == null) 
      { 
       if(m_msg.get(position).Recieved == 0) 
       { 
        convertView = inflater.inflate(R.layout.text_message_sent_row_template, null); 

        holder = new ViewHolder(); 
        holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1); 

        holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic); 
        holder.imgSend = (ImageView) convertView.findViewById(R.id.imgMessageSent); 

        holder.imgDeliverd = (ImageView) convertView.findViewById(R.id.imgMessageDeliverd); 
        holder.pbSending = (ProgressBar) convertView.findViewById(R.id.pbSendingMessage); 

        holder.spBubble.isChache = false; 
        holder.Position = position; 
        convertView.setTag(holder); 
        m_ViewHolder.add(holder); 
       } 
       else 
       { 
        convertView = inflater.inflate(R.layout.text_message_recieved_row_template, null); 

        holder = new ViewHolder(); 
        holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1); 
        holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic); 
        holder.spBubble.isChache = false; 
        holder.Position = position; 
        convertView.setTag(holder); 
        m_ViewHolder.add(holder); 
       } 
      } 

      Log.i("View Holder","Position After Find : " + position + " Wrong"); 
      Log.i("View Holder","Position After Find : " + holder.Position + " HolderPos"); 

      holder.spBubble.isChache = true; 

      //return v; 
     } 

     if(m_msg.get(position).Recieved == 0) 
     { 
      holder.spBubble.setBubbleDirectionLeft(false); 
      //speechBubble.setBubbleDirectionLeft(false); 
      holder.spBubble.setTextAlign(Align.RIGHT); 

      if (m_msg.get(position).Deliverd == 1) 
      { 
       holder.imgDeliverd.setVisibility(View.VISIBLE); 
       holder.imgDeliverd.setImageResource(R.drawable.message_deliverd); 
       holder.imgSend.setVisibility(View.GONE); 
       holder.pbSending.setVisibility(View.GONE); 
      } 
      else if (m_msg.get(position).Sent == 1) 
      { 
       holder.imgDeliverd.setVisibility(View.GONE); 
       holder.imgSend.setVisibility(View.VISIBLE); 
       holder.imgSend.setImageResource(R.drawable.message_sent); 
       holder.pbSending.setVisibility(View.GONE); 
      } 
     } 
     else 
     { 
      holder.spBubble.setBubbleDirectionLeft(true); 
      holder.spBubble.setTextAlign(Align.RIGHT); 
     } 

     CContacts_Info contact = cl_ContactInfo.GetContact(m_ContactILId); 

     holder.spBubble.setMessageBody(m_msg.get(position).MessageBody); 

     if(m_msg.get(position).MessageType.equals(AttachType.Emoji.name())) 
      holder.spBubble.setEmojiList(m_EmojiList); 
     else if(m_msg.get(position).MessageType.equals(AttachType.Picture.name())) 
     { 
      holder.spBubble.setImageSize(128, 128); 
      holder.spBubble.setImageReadyState(m_msgList.get(position).isImageReady); 
      if(m_msgList.get(position).isImageReady) 
       holder.spBubble.setPictureMessageThumbPic(m_msgList.get(position).ThumbPic); 
     } 

     holder.spBubble.setVisibility(View.VISIBLE); 
     holder.spBubble.setTextColor(Color.WHITE); 
     holder.spBubble.setFontSize(m_Default_FontSize); 

     holder.spBubble.setMessageId(m_msg.get(position)._id); 

     if (m_msg.get(position)._id == iMessageId) 
     { 
      if (m_msg.get(position).iProgressValue == 100) 
       m_Downloading = false; 
      else 
       m_msg.get(position).iProgressValue += 10; 

      holder.spBubble.SetProgress(m_msg.get(position).iProgressValue); 
     } 
     else 
      holder.spBubble.SetProgress(100); 

     //ImageView imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic); 
     //holder.imgProfilePic.setPadding(0, holder.spBubble.getHeight(), 0, 0); 

     if(contact.ProfilePic != null) 
     { 
      Bitmap bitmap = BitmapFactory.decodeByteArray(contact.ProfilePic, 0, 
        contact.ProfilePic.length,m_BitmapOPT); 

      holder.imgProfilePic.setImageBitmap(bitmap); 
     } 
     else 
     { 
      holder.imgProfilePic.setImageResource(R.drawable.profile_pic); 
     } 

     if (m_isStop) 
     { 
      holder.spBubble.setVoiceMessage_Play(false); 
      holder.spBubble.invalidate(); 
      m_isStop = false; 
     } 

     if (m_Playing) 
      UpdateTimer(holder.spBubble); 

     // UpdateTimer(holder.spBubble); 

     holder.spBubble.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       String messageType = m_msg.get(holder.Position).MessageType; 
       if (messageType.equals(AttachType.VoiceMessage.name())) 
       { 
        if (m_Playing) 
        { 
         stopPlaying(); 

         holder.spBubble.setVoiceMessage_Play(false); 
         holder.spBubble.invalidate(); 
        } 
        else 
        { 
         String source = holder.spBubble.getAttachSource(); 
         // Toast.makeText(Message_Activity.this, source, 
         // 1000).show(); 

         startPlaying(source); 
         ShowNotify(R.string.VoiceMessage_Play_Title); 

         update_Timer.run(); 

        } 
       } 
       else if (messageType.equals(AttachType.Audio.name())) 
       { 
        if (holder.spBubble.getProgress() < 100) 
         return; 
        String source = holder.spBubble.getAttachSource(); 
        Intent intent = new Intent(); 
        intent.setAction(android.content.Intent.ACTION_VIEW); 
        File file = new File(source); 
        intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
        startActivity(intent); 
       } 
       else if (messageType.equals(AttachType.Video.name())) 
       { 
        if (holder.spBubble.getProgress() < 100) 
         return; 
        String source = holder.spBubble.getAttachSource(); 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.parse(source), "video/*"); 
        startActivity(intent); 
       } 
       else if (messageType.equals(AttachType.Picture.name())) 
       { 
        String source = holder.spBubble.getAttachSource(); 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.parse("file:///" + source), "image/*"); 
        startActivity(intent); 
       } 
      } 
     }); 

     holder.spBubble.setOnLongClickListener(new OnLongClickListener() 
     { 

      @Override 
      public boolean onLongClick(View v) 
      { 

       List<Action_Type> actionList = CreatePopUpMenuList_Message(m_msg.get(holder.Position)._id); 
       ListView lstPopUpAction = (ListView) m_PopUpDialog.findViewById(R.id.listView1); 

       lstPopUpAction.setAdapter(new Show_Action_Adapter(Message_Activity.this, 
         actionList)); 

       m_PopUpDialog.show(); 
       return true; 
      } 
     }); 

     holder.spBubble.invalidate(); 

     //convertView.setTag(holder); 

     return convertView; 
    } 

но элемент, который показывает в ListView после прокрутки неправильно, иногда дублирует элемент и иногда неправильный порядок отображения позиции списка.
Я устанавливаю значение элемента после if/else, чтобы получить представление, но проблема не решена!
если я создать представление снова и не использовать для кэширования convertView (означает:

если (convertView == NULL

элемент отображается правильно, но если свиток много аварии приложения для получения слишком много памяти.
я совершенно запутался и не знаю, как я могу решить эту проблему.

+2

Это из-за ListView Recycling: [Как ListView Recycling Works] [1] [1]: http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works –

+0

@ Arash i can not получить решение из упоминания ссылки, можете ли вы объяснить, как я могу это решить? –

+0

можете ли вы разместить весь свой адаптер или, по крайней мере, ваш getview()? –

ответ

0

Попробуйте инстанцирование своего класса держателя внутри вашего GetView (я не могу увидеть, где вы инстанцированием что)

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

затем убедитесь, что ваш статический класс ViewHolder статичен, а также проверьте свой метод getCount(), чтобы вернуть правильный размер списка.

+0

Я создаю экземпляр моего держателя в своем адаптере, из getView –

Смежные вопросы