簡體   English   中英

nadavfima谷歌卡庫數據不顯示

[英]nadavfima google cards library data not displaying

我正在制作一個顯示天氣信息的應用程序。 為了顯示天氣,我正在使用nadavfima Google卡ui庫。 但是,當我嘗試在卡片布局中設置文本視圖時,即使在應用程序中刷新后也不會顯示。 它顯示的只是我在xml文件中設置的硬編碼值。

mCards = (CardUI) root.findViewById(R.id.nowCards);
    mCards.setSwipeable(false);

    // add cards

    // add overview card
    mCards.addCard(new NowOverviewCard());
    mCards.addStack(new CardStack());

    // add temperature
    mCards.addCard(new SimpleTempCard(getActivity()));
    mCards.addStack(new CardStack());

    // add detailed values
    SimpleValueCard pressure = new SimpleValueCard(getActivity());
    mCards.addCard(pressure);
    mCards.refresh();

布局視圖

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp" >

<TextView
    android:id="@+id/simpleCardHead"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:fontFamily="sans-serif-thin"
    android:text="Humidity"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/simpleCardValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/simpleCardHead"
    android:layout_alignBottom="@+id/simpleCardHead"
    android:layout_marginLeft="35dp"
    android:layout_toRightOf="@+id/simpleCardHead"
    android:fontFamily="sans-serif-thin"
    android:text="75%"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

我的卡視圖

public class SimpleValueCard extends Card {

View card;
private Context ctx;

public SimpleValueCard( Context ctx) {
this.ctx = ctx;
}

@Override
public View getCardContent(Context context) {
    return getViewFromContext(context);
}

@Override
public boolean convert(View convertCardView) {

    getViewFromContext(ctx);

    TextView header = (TextView) convertCardView
            .findViewById(R.id.simpleCardHead);
    TextView value = (TextView) convertCardView
            .findViewById(R.id.simpleCardValue);

    if (header == null || value == null) {

    } else {
        header.setText("Pressure");
        value.setText("84");
    }
    return true;
}

private View getViewFromContext(Context context) {
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    card = mInflater.inflate(R.layout.card_simple_value, null);

    return card;
}

}

問題是您正在調用getViewFromContext(ctx); 它返回視圖對象,但是不幸的是您沒有在convert(View convertCardView)中使用/保存該視圖對象。

因此,您需要存儲該返回的視圖對象,然后在您的convert(View convertCardView)中使用它。 就像自定義listview baseadapter一樣。 您需要借助該視圖對象來設置文本視圖。 因為在這里您要誇大而不是完全使用該視圖對象,所以它會顯示靜態值。

做以上事情,肯定會解決您的問題。

參考Android自定義ListView + BaseAdapter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM