2012-05-11 10 views
1

私は自宅のscrrenウィジェットを持っており、ウィジェットの内部にいくつかのアイテムを生成しています(私はこのアイテムのレイアウトを持っています)。 、最初のボタンの動作のためにのみ最初のリスナー他の人はdoesnの - 、ウィジェットに追加し、それは偉大に見えるが、リスナーが、私が望むように働いていない動的に作成されたアイテムのリスナー

RemoteViews currentItem1 = new RemoteViews(context.getPackageName(), R.layout.complex_list_item); 
    updateViews.addView(R.id.panelNews, currentItem1); 
    Intent intent1 = new Intent(context, ForexWidget.class); 
    intent1.setAction(Constants.ACTION_PRESSED_ITEM); 
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, 0); 
    updateViews.setOnClickPendingIntent(R.id.singleLine, pendingIntent1); 

    RemoteViews currentItem2 = new RemoteViews(context.getPackageName(), R.layout.complex_list_item); 
    updateViews.addView(R.id.panelNews, currentItem2); 
    Intent intent2 = new Intent(context, ForexWidget.class); 
    intent2.setAction(Constants.ACTION_PRESSED_ITEM); 
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, 0); 
    updateViews.setOnClickPendingIntent(R.id.singleLine, pendingIntent2); 

アイテムが作成されます。それはちょうどこのように、非常に簡単です't。私は、同じid(R.id.singleLine)の両方のボタンに対してsetOnClickPendingIntentを登録するので、私はそのことを想定していますが、最大の問題は、ボタンのidを設定することができないということです...

このようなことをする? Thx a lot

EDIT:これは私のレイアウトアイテムです、私は生成しようとしています。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/singleLine" 
    android:layout_width="fill_parent" 
    android:layout_height="34dp" 
    android:clickable="true" 
    android:orientation="vertical" > 

    <TextView 
      android:id="@+id/txtTime" 
      android:layout_width="45dp" 
      android:layout_height="wrap_content" 
      android:paddingRight="5dp" 
      android:textSize="@dimen/fontNewsCompact" /> 

     <TextView 
      android:id="@+id/txtCurrency" 
      android:layout_width="30dp" 
      android:layout_height="wrap_content" 
      android:paddingRight="5dp" 
      android:textSize="@dimen/fontNewsExtraCompact" /> 

</LinearLayout> 

答えて

2

問題は、あなたがそれを変更しない場合はそれだけで(あなたのフラグに応じて、または最後の)最初にかかりますが、アクションはそれぞれの意図のために異なっていなければならないということです。あなたのForexWidgetクラス利用の何かに続いて

intent1.setAction(Constants.ACTION_PRESSED_ITEM + "-1"); 
... 
intent2.setAction(Constants.ACTION_PRESSED_ITEM + "-2"); 

::私は似たような状況でやっていること

は、アクションなどに追加の一意の識別子を与えることである。また

if (intent.getAction().startsWith(Constants.ACTION_PRESSED_ITEM) { 

    // your logic here 
} 

、追加します同一のIDを持つオブジェクトに対する保留中の意図がある場合、できるだけ深い範囲に移動して、そのIDで1つのアイテムのみを囲むようにしてください。次のように意図を追加する必要がありその方法:

currentItem1.setOnClickPendingIntent(R.id.singleLine, pendingIntent1) 
... 
currentItem2.setOnClickPendingIntent(R.id.singleLine, pendingIntent2) 

あなたもあなたのリスト項目のレイアウト

+0

の範囲にある。このようにして、私のためにそれが仕事をdoesntの - もちろん、Imは用onClickPendingIntent 2倍を設定しているためuと同じidが見ることができます。一日の問題は、これを回避する方法です。詳細については、私は最初の投稿を更新し、生成しようとしているレイアウトを追加しました(id singleLineで) – qkx

+0

あなたは同じIDを使用していますが、それは異なっていなければならない。そのIDを使ってForexWidgetクラスのロジックを作成する場合は、等価ではなくaction.startsWith(Constants.Action_PRESSED_ITEM)を使用します。 – htafoya

+0

私は絶対に理解していますが、それは主要な問題ではありません。問題は、私はこれを 'updateViews.setOnClickPendingIntent(R.id.singleLine、pendingIntent1)'とこの 'updateViews.setOnClickPendingIntent(R.id.singleLine、pendingIntent2)'としているので、実際には私は2つのonclickインテントを1つ登録していますアイテム 'R.id.singleLine' - 私は2つのインスタンスを持っています。しかし、それを正しく登録するには?この 'updateViews.setOnClickPendingIntent(currentItem1.getLayoutId()、pendingIntent1)'とこの 'updateViews.setOnClickPendingIntent(currentItem2.getLayoutId()、pendingIntent2)'のようなものは当然動作しませんので... – qkx

関連する問題