0

私はListView用のカスタムアダプターを持っています
.getViewメソッドは次のようなものです:

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 の後に item の値をビューを取得するように設定しましたが、問題は解決しません!
ビューを再度作成し、convertView のキャッシュに使用しない場合 (意味:

if(convertView == null

アイテムは正しく表示されていますが、スクロールしすぎると、メモリが多すぎてアプリがクラッシュします。
私は完全に混乱していて、どうすれば解決できるかわかりません。

4

1 に答える 1

0

getView 内でホルダークラスをインスタンス化してみてください(インスタンス化する場所がわかりません)

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

次に、ViewHolder クラスが静的であることを確認し、getCount() メソッドをチェックして、適切なリスト サイズを返すようにします。

于 2013-08-03T11:17:42.777 に答える