更改Android中列表项的重力导致异常

时间:2016-11-13 20:18:21

标签: android listview classcastexception layoutparams

我有一个带有CursorAdapter的列表视图(不是simplecursordadpter),我试图改变列表项的重力。

问题似乎在线:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) row.getLayoutParams();

这是我的xml行项目列表:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:minWidth="100dp"
        android:background="@drawable/selector_items"
        android:descendantFocusability="blocksDescendants"
        android:padding="3dp"
        android:layout_margin="2dp">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:minWidth="280dp"
            android:background="@drawable/bolha_b"
            android:layout_gravity="start"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <TextView
                android:id="@+id/text_tit_mg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:shadowColor="@color/textShadow"
                android:shadowDx="1"
                android:shadowDy="1"
                android:text="teste"
                android:layout_weight="1"
                android:layout_gravity="center_horizontal"
                android:paddingRight="5dp"
                android:paddingLeft="5dp"
                android:layout_margin="5dp"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <ImageView
                android:id="@+id/img_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/bin"
                android:clickable="true"
                android:layout_gravity="right"
                android:layout_weight="0"

                />
           </LinearLayout>
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="horizontal">
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:paddingLeft="5dp"
               android:layout_marginLeft="5dp"
               android:text="De:"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/black"
               android:textStyle="bold"
               />
           <TextView
                android:id="@+id/txtfrom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:shadowColor="@color/textShadow"
                android:layout_marginLeft="5dp"
                android:text="nomefrom"
                android:lines="1"
                android:singleLine="true"
                android:ellipsize="end"
                android:textColor="@color/black"
                android:textAppearance="?android:attr/textAppearanceMedium" />

           </LinearLayout>

            <net.techabout.medappointment.TextViewFixTouchConsume
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_marginLeft="5dp"
                android:shadowColor="@color/textShadow"
                android:shadowDx="1"
                android:shadowDy="1"
                android:text=""
                android:textColorLink="@color/colorBackground"

                android:layout_weight="1"
                android:textColor="#040404"
                android:textAppearance="?android:attr/textAppearanceMedium" />


            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:text=""
                android:layout_marginRight="5dp"
                android:textColor="#999"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <Button
                android:id="@+id/but_answer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:text="Responder"
                android:background="@drawable/greenbutton"
                style="@style/MyButtonStyleSmall"
                android:layout_gravity="center_horizontal"

                />
        </LinearLayout>

</LinearLayout>

我在做:

public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) row.getLayoutParams();
        if (position % 2 == 0) {
            row.setBackgroundResource(R.drawable.bolha_a);//green
            params.gravity = Gravity.END;
            row.setLayoutParams(params);
            row.setPadding(50, 10, 20, 10);
        } else {
            row.setBackgroundResource(R.drawable.bolha_b);//white
            params.gravity = Gravity.START;
            row.setLayoutParams(params);
            row.setPadding(30, 10, 30, 10);
        }
        return row;
}

但我得到一个例外说:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.techabout.medappointment, PID: 11498
                  java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
                      at net.techabout.medappointment.MessagesCursorAdapter.getView(MessagesCursorAdapter.java:95)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                      at android.widget.ListView.makeAndAddView(ListView.java:1875)
                      at android.widget.ListView.fillSpecific(ListView.java:1354)
                      at android.widget.ListView.layoutChildren(ListView.java:1674)
                      at android.widget.AbsListView.onLayout(AbsListView.java:2148)

请帮忙。这是完整的代码:

public class MessagesCursorAdapter  extends CursorAdapter {
    /**
     * Cache of the children views 
     */
    public static class ViewHolder {
        public final Button butAnswer;
        public ImageView imgDelete;
        public final TextView mTitle;
        public final TextView mMsgFrom;
        public final TextView mMsg;
        public final TextView mTime;

        public ViewHolder(View view) {
            mTitle = (TextView) view.findViewById(R.id.text_tit_mg);
            mMsgFrom = (TextView) view.findViewById(R.id.txtfrom);
            mMsg = (TextView) view.findViewById(R.id.text1);
            mTime = (TextView) view.findViewById(R.id.text2);
        }
    }

    public MessagesCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int layoutId = R.layout.msg_list_item;
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) row.getLayoutParams();
        if (position % 2 == 0) {
            row.setBackgroundResource(R.drawable.bolha_a);//green
            params.gravity = Gravity.END;
            row.setLayoutParams(params);
            row.setPadding(50, 10, 20, 10);
        } else {
            row.setBackgroundResource(R.drawable.bolha_b);//white
            params.gravity = Gravity.START;
            row.setLayoutParams(params);
            row.setPadding(30, 10, 30, 10);
        }
        return row;
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        String temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_TIT));
        viewHolder.mTitle.setText(temp);

        temp = cursor.getString(cursor.getColumnIndexOrThrow(DataProvider.COL_MSG));

    }
}

1 个答案:

答案 0 :(得分:0)

将LinearLayout.LayoutParams更改为AbsListView.LayoutParams。