使用基础适配器在textview中添加图像

时间:2013-12-31 07:16:39

标签: android android-listview baseadapter

我正在进行聊天活动,用户输入要聊天的文字。现在我必须在聊天活动中实现附加图像。我已经制作了自定义基本适配器以在列表中包含消息。现在还没有想到如何在画廊和相机中附加照片,比如whatsapp。请帮帮我。我的代码是

PSAdapter.java

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;



@SuppressLint("ResourceAsColor")
public class PSAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<Message> mMessages;



    public PSAdapter(Context context, ArrayList<Message> messages) {
        super();
        this.mContext = context;
        this.mMessages = messages;
    }
    @Override
    public int getCount() {
        return mMessages.size();
    }
    @Override
    public Object getItem(int position) {       
        return mMessages.get(position);
    }
    @SuppressWarnings("deprecation")
    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message message = (Message) this.getItem(position);

        ViewHolder holder; 
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.sms, parent, false);
            holder.message = (TextView) convertView.findViewById(R.id.message_text);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        holder.message.setText(message.getMessage());

        LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();

        if(message.isStatusMessage())
        {
            holder.message.setBackgroundDrawable(null);
            lp.gravity = Gravity.LEFT;
            holder.message.setTextColor(R.color.textFieldColor);
        }
        else
        {       

            if(message.isMine())
            {
                holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
                lp.gravity = Gravity.RIGHT;
            }

            else
            {
                holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
                lp.gravity = Gravity.LEFT;
            }
            holder.message.setLayoutParams(lp);
            holder.message.setTextColor(R.color.textColor); 
        }
        return convertView;
    }
    public static class ViewHolder
    {
        TextView message;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

}

1 个答案:

答案 0 :(得分:0)

b1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent in = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(in, 0);
            }            
        });

通过这个你可以从gallery dude中选择文件。它只是意图你画廊和选择图像。 如果你有需要的话,请接受答案。告诉我任何问题。