listview notifyDataSetChanged()移动而不更改膨胀视图

时间:2013-05-20 12:14:17

标签: java android android-listview android-cursoradapter

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

这是我的活动

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();
        }
    }

}

当我打开Activity时,一切看起来都很好。插入消息显示左调整,外发调整右调整(基本)。当我有新消息时,我调用上面的代码来重新查询并更新列表。新项目的内容现在出现在我喜欢的bvottom上,但最后一项的布局似乎是最后一项。此新项目的布局显示在顶部。

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

由于

0 个答案:

没有答案