gpt4 book ai didi

android - 无法从 FirebaseMessagingService 将对象添加到自定义 ListView

转载 作者:行者123 更新时间:2023-11-30 00:23:58 26 4
gpt4 key购买 nike

最初,在设置自定义 ListView 后,没有更多的项目被添加,即显示在 ListView 中,尽管从 FirebaseMessagingService 添加了对象项目。我已将 listView 声明为静态,以便可以将对象从其他类或服务添加到列表中。这是我的代码:

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);
}
});
}

ListView Activity 代码:

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 格式的对象**还能够从 ListView Activity 中添加对象,但不能从 FirebaseMessagingSerivce 添加对象

最佳答案

I have declared listView static so that Object can be added to the list from other classes or services.

不是,这是一个很好的解决方案,您在这里泄漏 arrayList,因为当 Activity 被销毁时它不会被垃圾回收。

在这种情况下,更好的方法是使用 LocalBroadCast

查看链接以获取信息

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

现在,你做错了什么。您正在修改数组列表,但您没有通知适配器。

试试这个..

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 Activity 的 LocalBroadcastReceiver。
  • 使用 RecyclerView 而不是 ListView。

关于android - 无法从 FirebaseMessagingService 将对象添加到自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45742498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com