Android Notification自定义布局addView in循环

时间:2015-07-24 14:15:15

标签: java android notifications

我有货币价格和货币图标的JSONArray:

JSONArray prices = new JSONArray();
prices.put(new JSONObject("{'icon' : '" + R.drawable.ic_usd + "', 'price' : '" + btc_usd.price + "'}"));
prices.put(new JSONObject("{'icon' : '" + R.drawable.ic_eur + "', 'price' : '" + btc_eur.price + "'}"));
prices.put(new JSONObject("{'icon' : '" + R.drawable.ic_rur + "', 'price' : '" + btc_rur.price + "'}"));

然后我尝试使用RemoteViews添加:

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.price_notification_layout);

for(int i = 0; i < prices.length(); i++){
    try {
        RemoteViews newView = new RemoteViews(getPackageName(), R.layout.price_notification_layout);

        int icon = prices.getJSONObject(i).getInt("icon");
        String text = prices.getJSONObject(i).getString("price");

        newView.setImageViewResource(R.id.currencyIcon, icon);
        newView.setTextViewText(R.id.blankTextView, text);

        remoteViews.addView(R.id.currencyLayout, newView);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

price_notification_layout.xml:

<LinearLayout 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:id="@+id/currencyLayout"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/blankTextView"
        android:layout_gravity="center_horizontal" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/currencyIcon" />
</LinearLayout>

结果是JSONArray中的第一个图标和价格

其他结果没有添加周期,为什么?感谢。

0 个答案:

没有答案