繁体   English   中英

带XML布局的CardScrollView

[英]CardScrollView w/ xml layout

我已经阅读了许多文档,并且想知道为什么我的CardScrollAdapter实现为什么在getView()方法中返回NullPointerException,这使我非常震惊。 事实证明,我缺少setItemOnCard()-但是,从XE16开始,此方法已被getPosition()取代

这是我所拥有的:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Card card = new Card(context);
        if(convertView == null){
            //Inflate the layout to hold the card
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.alert_card, parent);
        }
        //populate the view
        TextView machine = (TextView) findViewById(R.id.alertMachine);
        TextView message = (TextView) findViewById(R.id.alertMessage);
        TextView id = (TextView) findViewById(R.id.alertId);
        TextView time = (TextView) findViewById(R.id.timestamp);
        message.setText("message");
        machine.setText("machine");
        id.setText("id");
        time.setText("time");
        return card.getView(convertView, parent);

我只是在尝试使用在XML布局中定义的卡加载ScrollView。

这是XML的样子

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout
        android:id="@+id/body_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/glass_card_body_height"
        android:layout_marginLeft="@dimen/glass_card_margin"
        android:layout_marginTop="@dimen/glass_card_margin"
        android:layout_marginRight="@dimen/glass_card_margin"
        tools:ignore="UselessLeaf"
        >

        <!-- Put your widgets inside this RelativeLayout. -->

        <TextView
            android:id="@+id/alertMachine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="18dp"
            android:text="No new alert"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/alertMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/alertMachine"
            android:layout_centerVertical="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/footer_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="@dimen/glass_card_margin"
        android:layout_marginBottom="@dimen/glass_card_footer_margin"
        android:layout_marginRight="@dimen/glass_card_margin"
        android:orientation="horizontal"
        >

        <!-- The footer view will grow to fit as much content as possible while the
             timestamp view keeps a fixed width. If the footer text is too long, it
             will be ellipsized with a 40px margin between it and the timestamp. -->

        <TextView
            android:id="@+id/alertId"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

        <TextView
            android:id="@+id/timestamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/glass_card_margin"
            android:ellipsize="end"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

    </LinearLayout>

</FrameLayout>

Card对象不满足某些要求,因为我用JSONObject填充了视图。 我有用于测试的值仍然会引发错误。 错误指向

message.setText("message");

NPE由TextView抛出setContentView()是使布局膨胀以查找子视图对象的原因。 在这种情况下,我设定

    scrollAdapter = new AlertScrollAdapter();
    cardScroller.setAdapter(scrollAdapter);
    if(cardScroller != null && !cardScroller.isActivated()){
        setContentView(cardScroller); <------
        cardScroller.activate();

然后,使用新展开的视图来查找子视图对象,然后用子对象将XML膨胀。

       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                Log.d(LOG_TAG, "Inflating view");
                //Inflate the layout to hold the card
                LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.alert_card, parent);
            }
            //populate the view
            TextView machine = (TextView) convertView.findViewById(R.id.alertMachine);
            TextView message = (TextView) convertView.findViewById(R.id.alertMessage);
            TextView id = (TextView) convertView.findViewById(R.id.alertId);
            TextView time = (TextView) convertView.findViewById(R.id.timestamp);


            if(cardList.size() != 0){
                try {
                    message.setText(cardList.get(position).getString(MESSAGE));
                    machine.setText(cardList.get(position).getString(MACHINE));
                    id.setText(cardList.get(position).getString(ID));
                    time.setText(cardList.get(position).getString(TIME));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{

            }
            return convertView;
        }

我返回的是膨胀视图,而不是

Card card = new Card(context);
return card.getView(convertView, parent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM