Android的简单光标适配器和多行布局

Android的简单光标适配器和多行布局,android,android-layout,android-listview,simplecursoradapter,Android,Android Layout,Android Listview,Simplecursoradapter,在过去的几个月里,我到处都在涉猎android,大部分只是教程。在我空闲时间学习的时候,我一直在为自己开发一个短信应用程序,我成功地为每一行设置了不同的布局,但它并没有像我预期的那样工作 根据光标所在位置的号码是否是我的电话号码,默认情况下,每一行都是左或右左 问题是,我的测试数据如下所示,其中绿色是我的文本: 以下是我的自定义适配器的代码: public class ConversationMessageListAdapter extends SimpleCursorAdapter {

在过去的几个月里,我到处都在涉猎android,大部分只是教程。在我空闲时间学习的时候,我一直在为自己开发一个短信应用程序,我成功地为每一行设置了不同的布局,但它并没有像我预期的那样工作

根据光标所在位置的号码是否是我的电话号码,默认情况下,每一行都是左或右左

问题是,我的测试数据如下所示,其中绿色是我的文本:

以下是我的自定义适配器的代码:

public class ConversationMessageListAdapter extends SimpleCursorAdapter {
    private LayoutInflater inflater;
    private String myPhoneNumber;

    public ConversationMessageListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
        super(context, layout, cursor, from, to, flags);
        myPhoneNumber = getMyPhoneNumber(context);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        String number = cursor.getString(2);

        System.out.println("phone#: "+number);
        System.out.println("my#: "+myPhoneNumber);

        if(number.equals(myPhoneNumber)){
            View view = inflater.inflate(R.layout.single_message_layout_right, null);
            return view;
        }

        return super.newView(context, cursor, parent);

    }

    public String getMyPhoneNumber(Context context){
        TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tMgr.getLine1Number();
    }


}
您可以看到我试图通过打印myPhoneNumber和number的值来调试问题。看起来它们并不总是与行正确匹配。它会在应该属于我的那一行上显示朋友的号码,但如果我保留默认布局,将它们全部保留在左侧,它会在textview的同一行上显示我的号码。比如:

555-555-FRND
Have you played Dynasty Warriors?
555-555-MINE
Not yet. Maybe soon.
但在这一点上,get打印的是:

phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-FRND
my# 555-555-MINE
当它看起来像:

phone#: 555-555-FRND
my# 555-555-MINE
phone#: 555-555-MINE
my# 555-555-MINE
我不知道是什么导致了这一切

如果有帮助,以下是我的setViewBinder:

textsDbAdapter.setViewBinder(new ConversationMessageListAdapter.ViewBinder() {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if(columnIndex == 2){
            String number = cursor.getString(columnIndex);
            String sentBy = getContactName(contentResolver, number);

            if(number.equals(myPhoneNumber)){
                return false;
            }

            TextView sender = (TextView) view;
            sender.setText(sentBy);
            return true;
        }

        //datetime
        if(columnIndex == 5){
            Long messageDatetime = Long.valueOf(cursor.getString(columnIndex));
            String receivedOn;

            if(startOfToday > messageDatetime){
                receivedOn = getDateFromDatetime(messageDatetime);
            } else {
                receivedOn = getTimeFromDatetime(messageDatetime);
            }

            TextView datetime = (TextView) view;
            datetime.setText(receivedOn);
            return true;
        }

        return false;
    }
});
编辑:我还发现,当我上下滚动时,行会随机对齐

EDIT2:添加了XML

单个消息布局右.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="5dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:layout_gravity="right"
    tools:context=".ConversationActivity">

    <!--Datetime Received-->
    <TextView
        android:id="@+id/datetime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_below="@+id/messageContent"
        android:layout_toLeftOf="@+id/displayPicContainer"
        android:textSize="14sp"
        android:textColor="#9110828f" />

    <!--Message-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageContent"
        android:textSize="15sp"
        android:layout_marginTop="5dp"
        android:layout_toLeftOf="@+id/displayPicContainer"/>

    <!--Display picture layout-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="3dp"
        android:layout_marginLeft="5dp"
        android:id="@+id/displayPicContainer">

        <!--Display picture-->
        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@drawable/bigboss150x150"
            android:id="@+id/displayPic"/>
    </LinearLayout>

</RelativeLayout>
单个消息\u布局\u left.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="5dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context=".ConversationActivity">

    <!--Display picture layout-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:layout_marginRight="5dp"
        android:id="@+id/displayPicContainer">

        <!--Display picture-->
        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@drawable/bigboss150x150"
            android:id="@+id/displayPic"/>
    </LinearLayout>

    <!-- sender -->
    <TextView
        android:id="@+id/sender"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:text="NAME"
        android:textColor="#ff9b9aa0"
        android:layout_toRightOf="@+id/displayPicContainer"/>

    <!--Datetime Received-->
    <TextView
        android:id="@+id/datetime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_below="@+id/messageContent"
        android:layout_toRightOf="@+id/displayPicContainer"
        android:textSize="14sp"
        android:textColor="#9110828f" />

    <!--Message-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageContent"
        android:textSize="15sp"
        android:layout_below="@+id/sender"
        android:layout_toRightOf="@+id/displayPicContainer"/>

</RelativeLayout>
conversation_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/conversationListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="43dp"
        android:stackFromBottom="true"
        android:transcriptMode="normal">
    </ListView>

    <RelativeLayout
        android:id="@+id/form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical"
        android:background="#ffffff">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:inputType="textCapSentences|textMultiLine"
            android:ems="10"
            android:lines="5"
            android:minLines="1"
            android:hint="Enter message"
            android:id="@+id/newMessageText"
            android:scrollHorizontally="true"
            android:scrollbars="vertical"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/sendButton"/>

        <Button
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Send"
            android:id="@+id/sendButton"
            android:layout_alignBottom="@+id/newMessageText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

我从newView切换到getView,这对我来说很有用

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row;

    Log.d("RowID", messages.getString(messages.getColumnIndex("_id")));

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    String fromNumber = messages.getString(messages.getColumnIndex("fromNumber"));

    if(fromNumber.equals(myPhoneNumber)){
        row = inflater.inflate(R.layout.single_message_layout_right, parent, false);
    } else {
        row = inflater.inflate(R.layout.single_message_layout_left, parent, false);
        TextView sender = (TextView) row.findViewById(R.id.sender);
        String name = getContactName(fromNumber);
        sender.setText(name);
    }

    TextView datetime = (TextView) row.findViewById(R.id.datetime);
    Long messageDatetime = messages.getLong(messages.getColumnIndex("datetime"));
    String receivedOn;
    if(startOfToday > messageDatetime){
        receivedOn = getDateFromDatetime(messageDatetime);
    } else {
        receivedOn = getTimeFromDatetime(messageDatetime);
    }
    datetime.setText(receivedOn);

    String message = messages.getString(messages.getColumnIndex("message"));
    TextView messageContent = (TextView) row.findViewById(R.id.messageContent);
    messageContent.setText(message);

    return row;
}

老实说,我不明白哪里出了问题,你打算实现什么,但有一件事我不喜欢:因为你不总是调用super.newView,它可能会卡在同一个光标位置。我会重写newView,每次调用super.newView,但如果您喜欢不同的布局,则忽略结果。问题是,事情没有正确对齐。如果查看图像,您可以看到红色和绿色部分的一些文本是如何左对齐的,一些文本是如何右对齐的。绿色部分应该在它自己的布局中向右对齐,而红色部分应该向左对齐。@JeremyJackson-请您发布该特定视图以及ListView的xml文件。我刚刚更新了它,以便您可以看到它。