0

我需要添加可以包含不同数量的 TextView 和 Imageview 对象的 ListView 项目。我该怎么做?以及如何确定和设置这些对象的 ID?当我尝试使用 ID R.id.chat_message 将 ImageView 或 TextView 添加到 LinearLayout 时,应用程序崩溃:(

package com.me.my_app;

import java.util.ArrayList;


import android.app.ListActivity;
import android.content.Context;
import android.graphics.BitmapFactory;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendSendMessageActivity extends ListActivity {

    ListView view;
    public ContactAdapter contactAdapter;

    LinearLayout LL;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_send_message);

        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            int index = Integer.parseInt(extras.get(getPackageName() + ".IndexInList").toString());
            String sentFrom = extras.get(getPackageName() + ".SentFrom").toString();
            int id = Integer.parseInt(extras.get(getPackageName() + ".Id").toString()); //либо id друга либо id друга
            FriendItem.Friend item = TabFriendsActivity.getList().get(index);

            view = getListView();

            contactAdapter = new ContactAdapter();
            contactAdapter.addItem(new MessageItem.Message(1, true, true, true, "123","13:31",new ArrayList<MessageItem.Message.Attachment>()));

            ArrayList<MessageItem.Message.Attachment> list = new ArrayList<MessageItem.Message.Attachment>();
            list.add(new MessageItem.Message.Attachment(MessageAttachmentType.PHOTO, RoundedImage.createRoundedPhoto(BitmapFactory.decodeResource(MainActivity.res,R.drawable.temp_user_photo))));

            contactAdapter.addItem(new MessageItem.Message(1, true, true, true, "123","9:10",list));
            view.setAdapter(contactAdapter);  
        }
    }





    class ContactAdapter extends BaseAdapter {

        private ArrayList<MessageItem.Message> mData = new ArrayList<MessageItem.Message>();
        private LayoutInflater mInflater;

         private static final int IGNORE_ITEM_VIEW_TYPE = -1;
         private static final int TYPE_ITEM = 0;    //обычный item
         private static final int TYPE_MAX_COUNT = 2;   //макс. число layout'ов отображения item'ов


        public ContactAdapter()
        {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


        public void addItem(final MessageItem.Message item) {
            mData.add(item);
            notifyDataSetChanged();
        }


        public int getItemViewType(int position) 
        {
            return TYPE_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }

        public int getCount()
        {
            return mData.size();
        }


        public MessageItem.Message getItem(int position)
        {
            return mData.get(position);
        }


        public long getItemId(int position)
        {
            return position;
        }

        public boolean isEnabled(int pos)
        {
            return true;
        }

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

            MessageItem.ViewHolder holder = null;

            MessageItem.Message item = mData.get(position);

            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


            if (convertView == null) 
            {
                holder = new MessageItem.ViewHolder();

                convertView = mInflater.inflate(item.in_out ? R.layout.chat_message_in_item : R.layout.chat_message_out_item,   null, false);

                if(item.listAttach.size() > 0)
                    holder.attachLL = (LinearLayout) findViewById(R.id.chat_message);




                for(MessageItem.Message.Attachment attach : item.listAttach)
                {   
                    switch(attach.type)
                    {
                    case PHOTO:
                        if(attach.photo != null)
                        {
                            ImageView iv = new ImageView(convertView.getContext());
                            //iv.setLayoutParams(params);
                            iv.setId(0x7f000000 - 1);
                            holder.listAttach.add(new MessageItem.Message.Attachment(attach.type,iv));
                            Log.v("","1" + iv.getId() + " " + holder.listAttach.size());
                        }
                        else
                            Log.v("", "Null!");
                        break;
                    }
                }

                convertView.setTag(holder);
            } else {
                holder = (MessageItem.ViewHolder) convertView.getTag();
            }



            for(int i = 0; i < holder.listAttach.size();i++)
            {
                if(holder.listAttach.get(i).type == MessageAttachmentType.PHOTO && item.listAttach.get(i).photo != null && holder.listAttach.get(i).image != null)
                {
                    holder.attachLL.addView(holder.listAttach.get(i).image);
                    holder.listAttach.get(i).image.setImageBitmap(item.listAttach.get(i).photo);
                }

                else
                    Log.v("", "Unable to add image!");
            }


            return convertView;
        }
    }





}

和 MessageItem.java:

package com.me.my_app;


import java.util.ArrayList;
import java.util.Comparator;

import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MessageItem
{
    public static class Message implements Comparator<Message> 
    {
        int id; 
        boolean in_out; 
        boolean is_read; 
        boolean is_delivered; 
        String message; 
        String time; 
        ArrayList<Attachment> listAttach; 
        public Message(int id, boolean in_out, boolean is_read, boolean is_delivered, String message, String time, ArrayList<Attachment> list)
        {
            this.id = id;
            this.in_out = in_out;
            this.is_read = is_read;
            this.is_delivered = is_delivered;
            this.message = message;
            this.time = time;
            listAttach = new ArrayList<Attachment>();
            if(list != null)
            {
                for(Attachment a : list)
                    listAttach.add(a);
            }
        }


        public int compare(Message object1, Message object2) {
            return object1.time.compareTo(object2.time);
        }
        public static class Attachment
        {
            ImageView image; 
            MessageAttachmentType type;
            Bitmap photo; 
            String Uri; 
            public Attachment(MessageAttachmentType type, Bitmap photo) 
            {
                this.type = type;
                this.photo = photo;
            }
            public Attachment(MessageAttachmentType type, ImageView image)
            {
                this.type = type;
                this.image = image;
            }
        }
    }

    public static class ViewHolder
    {   
        LinearLayout attachLL;
        TextView text, time;
        ArrayList<MessageItem.Message.Attachment> listAttach;
        public ViewHolder()
        {
            listAttach = new ArrayList<MessageItem.Message.Attachment>();
        }
    }
}
enter code here
4

1 回答 1

1

您很可能必须实现自己的列表视图适配器。然后,您可以为您的项目创建任何布局并进行设置。以下是它在我的代码中的完成方式:

public class ExpAdapter extends BaseExpandableListAdapter {

    /*SOME CODE HERE*/

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_row, null);
        }

        int groupId = ((ListTaskElement)this.getGroup(groupPosition)).id;       
        this.setupTaskView(convertView, this.childrenOfGroup(groupId, false).get(childPosition));

        convertView.setTag(this.getChildId(groupPosition, childPosition));
        return convertView;
    }

    /*SOME CODE HERE*/

}

您可以在此处找到所有详细信息:http: //jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

于 2012-06-21T11:13:05.447 回答