我想为 Android 中的聊天应用程序创建一个 ListView,在列表视图中,如果用户键入文本,我想显示文本布局,如果用户发送图像,则显示图像布局,我设计了两个布局,分别为 sms_row.xml 和 sms_chat_row.xml,如果我只使用 Text 和 Just Image ,该应用程序可以正常工作,通过手动设置我的条件 true 和 false ,但是如果我试图动态地这样做 getItemViewType(int position) 显示错误并且 Image crashed ,这是我的 Adapter 类代码-->
开发适配器.java
package com.example.imageinsertdynamic;
import java.util.ArrayList;
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.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class DevAdapter extends BaseAdapter {
private ArrayList<Message> list;
private Context mCont;
LayoutParams lp;
public Message ob;
int type;
public DevAdapter(Context context, ArrayList<Message> msg) {
super();
this.mCont = context;
this.list = msg;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(true)//if ob.issetStatus() true if user types and image if user selects an image from the directory. but getting nullPointerException here.
{
return 0;
}
else
{
return 1;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ob=(Message) this.getItem(position);
ViewHolder holder;
type=getItemViewType(position);//1 for case one and 2 for case 2 , need getItemViewtype() to bring this 1 and 2 but app crashes doing so need help in this function only
/*
*
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*
* HHHHHHHHEEEEEEEEEEEELLLLLLLLLLLPPPPPPPPPPP HERE !!
*
*
* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
*/
if(convertView == null)
{
holder = new ViewHolder();
switch (type) {
case 0:
convertView = LayoutInflater.from(mCont).inflate(R.layout.sms_row_chat, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.message_text);
convertView.setTag(holder);
break;
case 1:
convertView = LayoutInflater.from(mCont).inflate(R.layout.sms_row, parent, false);
holder.message = (ImageView) convertView.findViewById(R.id.message);
convertView.setTag(holder);
break;
}
}
else
holder = (ViewHolder) convertView.getTag();
switch(type){
case 0:
holder.text.setText(ob.getmsg());
System.out.println("value of isStatus image or text-->");
lp = (LayoutParams) holder.text.getLayoutParams();
break;
case 1:
holder.message.setImageBitmap(ob.getMessage());
System.out.println("value of isStatus image or text-->");
lp = (LayoutParams) holder.message.getLayoutParams();
break;
}
//Check whether message is mine to show green background and align to right
if(ob.isMine())
{ switch(type){
case 0:
holder.text.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
break;
case 1:
holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
break;
}
}
//If not mine then it is from sender to show orange background and align to left
else
{
switch(type){
case 0:
holder.text.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
break;
case 1:
holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
break;
}
}
switch(type){
case 0:
holder.text.setLayoutParams(lp);
break;
case 1:
holder.message.setLayoutParams(lp);
break;
}
//holder.message.setTextColor(R.color.textColor);
return convertView;
}
private static class ViewHolder
{
ImageView message;
TextView text;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
我已将返回值更改为 0 和 1 ,但是在 getItemViewType(int position) 中的 if 语句中,ob 对象是在 getView 中定义的,因此无法在 getItemView(int position) 中使用它在这里做什么才能使其正常工作?
public int getItemViewType(int position) {
if(ob.isStatusMessage)//if ob.issetStatus() true if user types and image if user selects an image from the directory. but getting nullPointerException here.
{
return 0;
}
else
{
return 1;
}
}
isStatusMessage 来自 --> Message.java
package com.example.imageinsertdynamic;
import android.graphics.Bitmap;
public class Message {
/**
* The content of the message
*/
Bitmap message;
String msg;
/**
* boolean to determine, who is sender of this message
*/
boolean isMine;
/**
* boolean to determine, whether the message is a status message or not.
* it reflects the changes/updates about the sender is writing, have entered text etc
*/
boolean isStatusMessage;
/**
* Constructor to make a Message object
*/
public Message(Bitmap message, boolean isMine,boolean status) {
super();
this.message = message;
this.isMine = isMine;
this.isStatusMessage = status;
}
/**
* Constructor to make a status Message object
* consider the parameters are swaped from default Message constructor,
* not a good approach but have to go with it.
*/
public Message(boolean status, String message,boolean isMine) {
super();
this.msg = message;
this.isMine = isMine;
this.isStatusMessage = status;
}
public Bitmap getMessage() {
return message;
}
public void setMessage(Bitmap message) {
this.message = message;
}
public String getmsg() {
return msg;
}
public void setMessage(String text) {
this.msg = text;
}
public boolean isMine() {
return isMine;
}
public void setMine(boolean isMine) {
this.isMine = isMine;
}
public boolean isStatusMessage() {
return isStatusMessage;
}
public void setStatusMessage(boolean isStatusMessage) {
this.isStatusMessage = isStatusMessage;
}
}