2017-08-17 13 views
1

最初は、カスタムリストビューを設定した後、FirebaseMessagingServiceのObjectアイテムを追加することを促すアイテムがリストビューに表示されなくなりました。 他のクラスやサービスのリストにObjectを追加できるように、私はlistView staticを宣言しました。 は、ここに私のコードです:FirebaseMessagingServiceからカスタムリストビューにオブジェクトを追加できません

FirebaseMessagingService:

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 

    //Toast.makeText(getApplicationContext(), remoteMessage.getData().get("transaction"),Toast.LENGTH_SHORT).show(); 
    Handler handler = new Handler(Looper.getMainLooper()); 
    handler.post(new Runnable() { 

     @Override 
     public void run() { 

      Gson gson = new Gson(); 
      Block b = gson.fromJson(remoteMessage.getData().get("transaction"), Block.class); 
      OpenChain.arrayList.add(b); 
     } 
    }); 
} 

リストビューアクティビティコード:

public static ArrayList<Block> arrayList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_open_chain); 

    arrayList = new ArrayList<>(); 

    getSupportActionBar().setTitle("Vote Ledger"); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    ListView listView = (ListView) findViewById(R.id.listView); 

    BlockchainAdap adap = new BlockchainAdap(this, arrayList); 

    listView.setAdapter(adap); 
    adap.notifyDataSetChanged(); 

} 

**私はJSON形式 でクラウドからオブジェクトを受け付けております**また、内からオブジェクトを追加することができリストビューのアクティビティはFirebaseMessagingSerivceからではありません。

答えて

1

I listView staticを宣言して、他のクラスやサービスの リストにObjectを追加できるようにしました。

ない、良い解決策活動が破壊されますとき、それはゴミを収集すること文句を言わないように、あなたは、ここにのArrayListをリークしています。

このシナリオでは、LocalBroadCastを使用することをお勧めします。

アウト

https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

情報

のリンクは、あなたは間違ってやっています。あなたは、arraylistを変更していますが、同じことについてアダプターに通知していません。

はこれを試してみてください。..

private ArrayList<Block> arrayList = new ArrayList<>(); 
private BlockchainAdap adap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_open_chain); 

    getSupportActionBar().setTitle("Vote Ledger"); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    ListView listView = (ListView) findViewById(R.id.listView); 

    adap = new BlockchainAdap(this, arrayList); 

    listView.setAdapter(adap); 
} 

public static void updateList(Block b){ 
    arrayList.add(b); 
    adap.swap(arrayList); 
} 
FirebaseMessagingService

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 
      Gson gson = new Gson(); 
      Block b = gson.fromJson(remoteMessage.getData().get("transaction"), Block.class); 
      OpenChain.updateList(b); 
} 

また、あなたの** BlockchainAdap **スワップのためのメソッドを公開します。

class BlockchainAdap { 
    ArrayList<Block> arrayList; 
    BlockchainAdap(ArrayList<Block> arrayList){ 
    this.arrayList = arrayList; 
    } 

    public void swap(ArrayList<Block> arrayList){ 
    this.arrayList = arrayList; 
    notifydatasetChanged(); 
    } 

    // other methods 

} 

これは動作しますが、OpenChain活動へのメッセージングサービスから

  • LocalBroadcastReceiverを使用します。
  • ListViewではなくRecyclerViewを使用します。
関連する問題