我正在创建一个Android应用,其中我有一个带聊天气泡(气泡)的列表视图我想在这些聊天气泡中添加图像,如果用户从选项插入图像,我的应用看起来像这样 - &gt ;
我想在应用程序中插入此图像,当用户通过图像按钮插入时,我已经自己定制了适配器,请查看我的AwesomeAdapter类和Main_activity
Main_Activity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
text = (EditText) this.findViewById(R.id.txtMessage);
sndButton=(Button)findViewById(R.id.buttonchat3);
//sndButton.setEnabled(false);
messages = new ArrayList<Message>();
adapter = new AwesomeAdapter(this, messages);
setListAdapter(adapter);
}
public void sendMessage(View v)
{
String newMessage = text.getText().toString().trim();
if(newMessage.length() > 0)
{
text.setText("");
addNewMessage(new Message(newMessage, true));
mConnection.sendTextMessage(newMessage);
}
}
public void receiveImage(View v)
{
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,RESULT_LOAD_IMAGE);
}
void addNewMessage(Message m)
{
messages.add(m);
adapter.notifyDataSetChanged();
getListView().setSelection(messages.size()-1);
}
@SuppressWarnings("static-access")
void addReceivedMessage(Message m)
{
messages.add(m);
adapter.notifyDataSetChanged();
getListView().setSelection(messages.size()-1);
v=(Vibrator) getSystemService(this.VIBRATOR_SERVICE);
v.vibrate(200);
}
AwesomeAdapter.java 这是列表视图的Adapter类,我在后台显示了这个文本框和9个补丁图片,
公共类AwesomeAdapter扩展了BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private Context mContext;
private ArrayList<Message> mMessages;
//private TreeSet mSeparatorsSet = new TreeSet();
public AwesomeAdapter(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);
}
@Override
public int getItemViewType(int position) {
if(my code to get text content) //it will fetch the textview layout
{
return TYPE_SEPARATOR;
}
else //it will fetch the image view layout
{
return TYPE_ITEM;
}
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@SuppressLint("ResourceAsColor")
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = (Message) this.getItem(position);
ViewHolder holder;
int type = getItemViewType(position);
if(convertView == null)
{
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.message_text);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row2, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.message2);
break;
}
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.message.setText(message.getMessage());
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
//check if it is a status message then remove background, and change text color.
if(message.isStatusMessage())
{
holder.message.setBackgroundDrawable(null);
lp.gravity = Gravity.LEFT;
holder.message.setTextColor(R.color.textFieldColor);
}
else
{
//Check whether message is mine to show green background and align to right
if(message.isMine())
{
holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
}
//If not mine then it is from sender to show orange background and align to left
else
{
holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
}
holder.message.setLayoutParams(lp);
holder.message.setTextColor(R.color.textColor);
}
return convertView;
}
private static class ViewHolder
{
TextView message;
}
@Override
public long getItemId(int position) {
//Unimplemented, because we aren't using Sqlite.
return 0;
}
}
我已经制作了sms_row1和sms_row2文件,现在我应该如何开始这个另一个View的工作,我是新手android尝试了很多修改,但它仍然无法正常工作,请提前帮助和thanx。 / p>
答案 0 :(得分:0)
我的朋友,在我看来。解决方案是使用带有image和textView以及第三个布尔变量的自定义适配器。
private Context mContext;
private ArrayList<Message> mMessages;
private boolean isText;
//private TreeSet mSeparatorsSet = new TreeSet();
public AwesomeAdapter(Context context, ArrayList<Message> messages, boolean isText) {
super();
this.mContext = context;
this.mMessages = messages;
this.isText = isText;
}
这个“isText”变量将决定显示文字还是图像。 我希望它能起作用:))