0

我正在使用ListViewwithCursorAdapter来显示一些信息。根据信息类型,行是不同的,但实际上有些项目在右边,有些在左边。

这是我的活动

package ;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.QuickContactBadge;

public class ConversationActivity extends BaseActivity implements
        OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
    private static final String TAG = ConversationActivity.class
            .getSimpleName();
    Cursor cursor; // 1
    ListView conversationList; // 2
    Button sendButton;
    EditText newMessageText;
    QuickContactBadge badge;
    static String ID;


    ConversationReceiver conversationReceiver = new ConversationReceiver();

    private static final int LOADER_ID = 0x02;
    private ConversationAdapter adapter;

    @SuppressLint("NewApi")
    @Override
    protected void onStart() {
        super.onStart();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.conversation);

        getSupportLoaderManager().initLoader(LOADER_ID, null, this);

        // Die Views suchen
        conversationList = (ListView) findViewById(R.id.ConversationList);
        sendButton = (Button) findViewById(R.id.Conv_buttonSendMessage);
        newMessageText = (EditText) findViewById(R.id.Conv_newMessageText);

        Intent intent = getIntent();
        ID = intent.getStringExtra("ID");

        sendButton.setOnClickListener(this);



        try {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
                Log.d(TAG, "Erstelle ActionBar fuer ID: " + ID);

                String[] projection = new String[] {
                        ContactsContract.PhoneLookup.DISPLAY_NAME,
                        ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI };

                ContentResolver resolver = getContentResolver();
                String[] br_projection = new String[] { BaseColumns._ID,
                        "conv_type", "address", "Profile_ID" };
                Cursor ConvCursor = resolver
                        .query(Uri
                                .parse("content://.../conversations"),
                                br_projection, BaseColumns._ID + " = ?",
                                new String[] { ID }, null);
                ActionBar actionBar = getActionBar();

                if (ConvCursor.moveToFirst()) {
                    String number = "";

                    number = ConvCursor.getString(ConvCursor
                            .getColumnIndexOrThrow("address"));

                    Log.v(TAG, "Nummer: " + number);
                    Uri contactUri = Uri.withAppendedPath(
                            PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

                    Cursor contactCursor = getContentResolver().query(
                            contactUri, projection, null, null, null);
                    Log.v(TAG, "contactCursor.moveToFirst");
                    if (contactCursor.moveToFirst()) {

                        // Get values from contacts database:
                        String ContactName = contactCursor
                                .getString(contactCursor
                                        .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

                        String ContactThumbnailString = contactCursor
                                .getString(contactCursor
                                        .getColumnIndex(ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI));

                        ImageView logo = (ImageView) findViewById(android.R.id.home);

                        if (ContactThumbnailString != null) {
                            Uri ContactThumbnailURI = Uri
                                    .parse(ContactThumbnailString);
                            logo.setImageURI(ContactThumbnailURI);
                        }

                        actionBar.setTitle(ContactName);

                    } else {

                        actionBar.setTitle(R.string.ContactNoName);
                    }
                    actionBar
                            .setSubtitle(PhoneNumberUtils.formatNumber(number));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "ConvAct Header Info ", e);
        }

        adapter = new ConversationAdapter(this, cursor);
        conversationList.setAdapter(adapter);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");


    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(conversationReceiver);
    }

    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        String[] projection = new String[] { BaseColumns._ID, "item_type",
                "send", "serverid", "direction", "date", "content", "sender",
                "Conv_ID" };
        Intent intent = getIntent();

        return new CursorLoader(
                this,
                Uri.parse("content://.../items"),
                projection, "Conv_ID = ?", new String[] { intent
                        .getStringExtra("ID") }, "date ASC");
    }

    @SuppressLint("NewApi")
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.swapCursor(cursor);
    }

    @SuppressLint("NewApi")
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        adapter.swapCursor(null);
    }

    // Wird bei Klicks auf den Button aufgerufen //
    public void onClick(View v) {
        Log.d(TAG, "Neues Item fuer ID: "+ID);


        ContentResolver resolver = getContentResolver();
        Cursor convCursor = resolver
                .query(Uri
                        .parse("content://.../conversations/"
                                + ID), new String[] { "address" }, null, null,
                        null);

        convCursor.moveToFirst();

        ContentValues values = new ContentValues();
        values.clear();
        values.put("item_type", "tm");
        values.put("direction", "out");
        values.put("send", "0");
        values.put("date", String.valueOf(System.currentTimeMillis()));
        values.put("content", newMessageText.getText().toString());
        values.put("sender",
                convCursor.getString(convCursor.getColumnIndex("address")));
        values.put("Conv_ID", ID);
        resolver.insert(Uri
                .parse("content://.../items"),
                values);




        newMessageText.setText("");

    }

}

这是我的一部分CursorAdapter

package ;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.QuickContactBadge;
import android.widget.TextView;

public class ConversationAdapter extends CursorAdapter {
    private static final String TAG = ConversationAdapter.class.getSimpleName();

    @SuppressWarnings("deprecation")
    public ConversationAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        String item_direction = cursor.getString(cursor
                .getColumnIndexOrThrow("direction"));
        View v = null;

        if (item_direction.equals("out")) {
            v = inflater.inflate(R.layout.conversationrow_out, parent, false);

        } else if (item_direction.equals("in")) {
            v = inflater.inflate(R.layout.conversationrow_in, parent, false);

        }

        bindView(v, context, cursor);

        return v;

    }

    @SuppressLint({ "InlinedApi", "NewApi" })
    @Override
    public void bindView(View row, Context context, Cursor cursor) {
        String item_type = cursor.getString(cursor
                .getColumnIndexOrThrow("item_type"));
        Log.v(TAG, item_type);
        Cursor contactCursor = null;

        try {
            TextView ConversationText = (TextView) row
                    .findViewById(R.id.ConversationText);

            Log.v(TAG, "message selected");
            // Definition
            String[] projection;
            String number, textContent = "";
            Uri ThumbnailURI = null;
            String ThumbnailString;

            QuickContactBadge ConversationBadge = null;

            Log.d(TAG,
                    "Nachricht mit Inhalt: "
                            + cursor.getString(cursor
                                    .getColumnIndexOrThrow("content"))
                            + " in Richtung: "
                            + cursor.getString(cursor
                                    .getColumnIndexOrThrow("direction")));

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
                projection = new String[] {
                        ContactsContract.PhoneLookup.DISPLAY_NAME,
                        ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI };

                ConversationBadge = (QuickContactBadge) row
                        .findViewById(R.id.ConversationBadge);
            } else {
                projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME };
            }

            Log.v(TAG, "Lese nun Nutzerdaten");
            number = cursor.getString(cursor.getColumnIndexOrThrow("sender"));
            Uri contactUri = Uri.withAppendedPath(
                    PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

            Log.v(TAG, "Nutzerdaten-Query");

            contactCursor = context.getContentResolver().query(contactUri,
                    projection, null, null, null);

            if (contactCursor.moveToFirst()) {

                String name = contactCursor
                        .getString(contactCursor
                                .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
                    ConversationBadge.assignContactFromPhone(number, true);

                    ThumbnailString = contactCursor
                            .getString(contactCursor
                                    .getColumnIndex(ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI));

                    if (cursor.getString(
                            cursor.getColumnIndexOrThrow("direction")).equals(
                            "in")
                            && ThumbnailString != null) {
                        ThumbnailURI = Uri.parse(ThumbnailString);
                        ConversationBadge.setImageURI(ThumbnailURI);
                    } else {
                        ConversationBadge.setImageToDefault();
                    }
                }

                // textContent = name+": ";

            } else {
                // textContent = number+": ";

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {

                    ConversationBadge.setImageToDefault();
                }

            }

            if (item_type.equals("tm")) {
                textContent += (cursor.getString(cursor
                        .getColumnIndexOrThrow("content")));

            }

            ConversationText.setText(textContent);

            TextView ConversationTime = (TextView) row
                    .findViewById(R.id.ConversationTime);

            long timestamp = cursor.getLong(cursor.getColumnIndex("date"));

            ConversationTime.setText(DateUtils
                    .getRelativeTimeSpanString(timestamp));

        } catch (Exception e) {
            Log.e(TAG, "bindView ", e);
        } finally {
            contactCursor.close();
        }
    }

}

当我打开活动时,一切看起来都很好。传入消息显示为左调整,传出消息右调整(基本上)。当我收到新消息时,我会调用上面的代码来重新查询和更新列表。新项目的内容现在出现在我喜欢的 bvottom 上,但最后一个项目的布局似乎是最初的最后一个。此新项目的布局显示在顶部。

我想发布图片,但我没有足够的声望点。:(

谢谢

4

0 回答 0