如何在聊天适配器中显示已传递的消息

时间:2014-10-13 09:30:18

标签: android

这是我的 Mainactivity类使用此代码的代码可以显示已发送的邮件和失败

@Override
        public void executionResult(int result) {
            // TODO Auto-generated method stub
        final int  delivered=result;
            runOnUiThread(new  Runnable() {
                public void run() {
                    if(delivered==1)
                    {
                        Toast.makeText(getApplicationContext(), "sent", 1000).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "failure", 1000).show();
                    }

                }
            });
        }

虽然我必须在适配器中显示它,但我已创建了在文本视图中传递的字段,但在成功发送消息时无法在适配器中显示。以下是该适配器的适配器 xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/even_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <com.lociiapp.utils.RoundedImageView
        android:id="@+id/odd_bubble"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dip"
        android:src="@drawable/index"
        android:visibility="visible" />

    <com.lociiapp.utils.RoundedImageView
        android:id="@+id/user_img"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:layout_margin="5dip"
        android:src="@drawable/index"
        android:visibility="visible" />

    <LinearLayout
        android:id="@+id/shareRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_toLeftOf="@+id/user_img"
        android:layout_toRightOf="@+id/odd_bubble"
        android:background="@drawable/rectangle"
        android:gravity="right"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="30dp"
            android:textColor="#636363"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/delivered"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <ImageView
        android:id="@+id/leftimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/shareRow"
        android:layout_alignTop="@+id/user_img"
        android:layout_marginRight="-11.5dip"
        android:src="@drawable/callout_right" />

    <ImageView
        android:id="@+id/ rightangale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/odd_bubble"
        android:layout_marginLeft="19dip"
        android:layout_toRightOf="@+id/odd_bubble"
        android:src="@drawable/callout_left" />

</RelativeLayout>

传递textview是adepter的ID,如果有1,我必须显示已发送和失败的消息:

public class ChatAdapter extends ArrayAdapter<Chat> {
    private final Context context;
    private final ArrayList<Chat> values;
    ImageLoader imageloader;
    Datamodel dm;

    public ChatAdapter(Context context, ArrayList<Chat> values) {
        super(context, R.layout.list_row_layout_even, values);
        this.context = context;
        this.values = values;
        imageloader = new ImageLoader(context);
    }

    public void addMessage(Chat chat) {
        values.add(chat);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row_layout_odd,
                    parent, false);

        RelativeLayout root = (RelativeLayout) convertView
                .findViewById(R.id.even_container);
        TextView tv = (TextView) convertView.findViewById(R.id.text);

        TextView delivred = (TextView) convertView.findViewById(R.id.delivered);

        RoundedImageView userImg = (RoundedImageView) convertView
                .findViewById(R.id.user_img);
        RoundedImageView oddImg = (RoundedImageView) convertView
                .findViewById(R.id.odd_bubble);
        ImageView leftimage = (ImageView) convertView
                .findViewById(R.id.leftimage);
        ImageView rightimage = (ImageView) convertView
                .findViewById(R.id.rightangale);
        Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
                "fonts/ARIAL.TTF");
        SharedPreferences prefs = context.getSharedPreferences(
                AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
        String K = prefs.getString("Member_id", "");
        Chat chat = values.get(position);
        String t = prefs.getString("PREFS_NAME_reccvierChatadpter", "");
        tv.setText(chat.getMessage());
        tv.setTypeface(fontArial);
        AQuery aq = new AQuery(context);

        if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
            root.setBackgroundColor(Color.parseColor("#ffffff"));

            tv.setTextColor(Color.parseColor("#636363"));
            userImg.setVisibility(View.VISIBLE);
            rightimage.setVisibility(View.GONE);
            leftimage.setVisibility(View.VISIBLE);
            aq.id(userImg).image(
                    "http://api.lociiapp.com/TransientStorage/" + K + ".jpg");

            oddImg.setVisibility(View.GONE);
        } else {
            root.setBackgroundColor(Color.parseColor("#f5f6f1"));
            tv.setTextColor(Color.parseColor("#bdbdbd"));
            aq.id(oddImg).image(
                    "http://api.lociiapp.com/TransientStorage/" + t + ".jpg");
            userImg.setVisibility(View.GONE);
            oddImg.setVisibility(View.VISIBLE);
            leftimage.setVisibility(View.GONE);
            rightimage.setVisibility(View.VISIBLE);

        }

        return convertView;
    }
}

这是适配器的代码。请帮助并建议我在哪里改变我无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试使用Handler而不是runOnUiThread。你是否测试过你在&#34; executionResult(int result)&#34中得到结果??