2015-01-30 2 views
-2

Я пытаюсь сделать приложение чата в android, используя xmpp. Я могу общаться с простым адаптером массива, но когда я пытаюсь установить пользовательский адаптер в списке, я получаю ошибку в своем коде. пожалуйста помогите. где я ошибаюсь.пользовательский адаптер с девятью изображениями патча

Мой код пользовательского адаптера:

public class ChatArrayAdapter extends ArrayAdapter 
{ 
    private TextView chatText; 

    private LinearLayout singleMessageContainer; 

    private Activity activity; 

    public ChatArrayAdapter(Context context, int textViewResourceId, List chatList) { 
     super(context, textViewResourceId, chatList); 
    } 


    public void add(ChatMessage object) { 
     chatMessageList.add(object); 
     super.add(object); 
    } 

    public ChatArrayAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 


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

    public ChatMessage getItem(int index) { 
     return (ChatMessage) chatMessageList.get(index); 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.chat_singlemessage, parent, false); 
     } 
     singleMessageContainer = (LinearLayout) row.findViewById(R.id.singleMessageContainer); 
     ChatMessage chatMessageObj = getItem(position); 
     chatText = (TextView) row.findViewById(R.id.singleMessage); 
     chatText.setText(chatMessageObj.message); 
     chatText.setBackgroundResource(chatMessageObj.left ? R.drawable.bubbleaaa : R.drawable.bubblebbb); 
     singleMessageContainer.setGravity(chatMessageObj.left ? Gravity.LEFT : Gravity.RIGHT); 
     return row; 
    } 

    public Bitmap decodeToBitmap(byte[] decodedByte) { 
     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
    } 


} 

ChatMessage класс:

public class ChatMessage 
{ 
    public boolean left; 
public String message; 

public ChatMessage(boolean left, String message) { 
    super(); 
    this.left = left; 
    this.message = message; 
} 
} 

Ошибки:

01-30 14:39:05.663 10813-10813/com.example.welcome.kids_chat E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.welcome.kids_chat, PID: 10813 
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.welcome.kids_chat.ChatMessage 
     at com.example.welcome.kids_chat.Chat_Screen$ChatArrayAdapter.getItem(Chat_Screen.java:307) 
     at com.example.welcome.kids_chat.Chat_Screen$ChatArrayAdapter.getView(Chat_Screen.java:317) 
     at android.widget.AbsListView.obtainView(AbsListView.java:2301) 
     at android.widget.ListView.measureHeightOfChildren(ListView.java:1274) 
     at android.widget.ListView.onMeasure(ListView.java:1186) 
     at android.view.View.measure(View.java:16677) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286) 
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
     at android.view.View.measure(View.java:16677) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at android.view.View.measure(View.java:16677) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286) 
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
     at android.view.View.measure(View.java:16677) 
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286) 
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421) 
     at android.view.View.measure(View.java:16677) 
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1927) 
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1119) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5652) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
     at android.view.Choreographer.doFrame(Choreographer.java:544) 
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5426) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
     at dalvik.system.NativeStart.main(Native Method) 
+0

I гу ess есть ошибка в вашем типе chatMessageList, где вы его объявили? –

+0

Можете ли вы показать нам декларацию e в файле arrayapdapter и chatMessageList. – Flaxie

+0

private Список chatMessageList = новый ArrayList(); – nav123

ответ

0

попытка изменить Список chatList в Список <ChatMessage> chatList и а также Список <ChatMessage> chatMessageList

0

Написать

private List<ChatMessage> chatMessageList = new ArrayList(); 

и объявить адаптер, как

public class ChatArrayAdapter extends ArrayAdapter<ChatMessage>{ 

это сделает arrayadapter по умолчанию для обработки ChatMessage.

Вы можете удалить

public void add(ChatMessage object) { 
    chatMessageList.add(object); 
    super.add(object); 
} 

и

public ChatMessage getItem(int index) { 
    return (ChatMessage) chatMessageList.get(index); 
} 

они будут автоматически иметь вид ChatMessage

+0

, когда я применяю этот код, а затем в другой части кода, т.е. chatMessageList.add (message.getBody()); m get error add (com.example.welcome, kids_chat.ChatMessage) в List нельзя применить к (java.lang.string) – nav123

+0

Является ли message.getBody() возвращающей строку, может быть? – Flaxie

0

Использование BaseAdapter с ViewHolder дизайн шаблона:

public class ChatAdapter extends BaseAdapter 
{ 
    private Context context; 
    private List<ChatMessage> chatList; 

    public ChatAdapter(Context context,List<ChatMessage> chatList) { 
     this.context=context; 
     this.chatList=chatList; 
    } 


    public void add(ChatMessage object) { 
     if(chatList==null){ 
      chatList = new ArrayList<ChatMessage>(); 
     } 
     chatList.add(object); 
     notifyDataSetChanged(); 
    } 

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

    @Override 
    public ChatMessage getItem(int position) { 
     return chatList.get(position); 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(R.layout.chat_singlemessage, null, false); 
      holder.chatText = (TextView) convertView.findViewById(R.id.singleMessage); 
      holder.singleMessageContainer = (LinearLayout) convertView.findViewById(R.id.singleMessageContainer); 
      convertView.setTag(holder); 
     }else{ 
      holder =(ViewHolder) convertView.getTag(); 
     } 
     holder.chatText.setText(chatList.get(position).message); 
     holder.chatText.setBackgroundResource(chatList.get(position).left ? context.getResources(R.drawable.bubbleaaa) : context.getResources(R.drawable.bubbleaaa)); 
     holder.singleMessageContainer.setGravity(chatList.get(position).left ? Gravity.LEFT : Gravity.RIGHT); 
     return convertView; 
    } 

    class ViewHolder{ 
     TextView chatText; 
     LinearLayout singleMessageContainer; 
    } 

    public Bitmap decodeToBitmap(byte[] decodedByte) { 
     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
    } 
} 
+0

получение такой же ошибки с этим также – nav123

Смежные вопросы