我的片段缓存了数据并且没有刷新

时间:2018-09-26 13:10:45

标签: android listview fragment

我正在尝试在出现推送通知时更改片段的数据,并且每当接收到推送通知时我都会调整ListView,但是我的列表不会更改,并且当我转到另一个片段然后返回到该片段时,它的更改并且似乎该应用程序正在使用XML缓存,因此无法动态,实时地进行更改。 有谁知道我该如何解决? 任何帮助都感激不尽。

这是我的categoryFragment.java

public class categoryFragment extends Fragment{
ArrayList<HashMap<String,String>> contact=new ArrayList<>();
public static final String ARG_OBJECT = "object";
private String TAG = DemoObjectFragment.class.getSimpleName();

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    Bundle args = getArguments();
    ArrayList<HashMap<String,String>> arrayList= (ArrayList<HashMap<String,String>>)getArguments().getSerializable("key");
    contact.clear();

    for (int i = 0; i < arrayList.size(); i++) {

        if (arrayList.get(i).get("assetId").equals(MainActivity.arrayListSpinner.get(args.getInt(ARG_OBJECT)).getIdAsset()+"")
                &&!arrayList.get(i).get("sourceName").equals("null")){
            contact.add(arrayList.get(i));


        }

    }
    View header = getLayoutInflater().inflate(R.layout.header_price_list, null);

    View rootView = inflater.inflate(R.layout.coin, container, false);
    ListView lv = rootView.findViewById(R.id.list1);
    lv.setAdapter(null);
    lv.addHeaderView(header);
    ListAdapter adapter = new SimpleAdapter(getActivity()
            , contact,
            R.layout.list_item, new String[]{"sourceName", "priceCurrency"},
            new int[]{R.id.sourceName, R.id.priceCurrency});
    lv.setAdapter(adapter);
    return rootView;
}

}

这是我的pageadapter.java

public class categoryPagerAdapter extends FragmentStatePagerAdapter {

public categoryPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    Fragment fragment = new categoryFragment();
    Bundle args = new Bundle();
    // Our object is just an integer :-P
    if (i==MainActivity.arrayListSpinner.size()){
        i=i-1;
    }
    args.putInt(categoryFragment.ARG_OBJECT, i);

    args.putSerializable("key",MainActivity.contactList);
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getCount() {
    return 100;
}

@Override
public CharSequence getPageTitle(int position) {
    return "OBJECT " + (position + 1);
}
}

此notificationClass.java

public class PushReceiver extends BefrestPushReceiver {
    String notify = "";
    private String TAG = PushReceiver.class.getSimpleName();

    @Override
    public void onPushReceived(Context context, BefrestMessage[] messages) {
        JSONObject jsonObj = null;
        JSONArray contacts = null;
        try {
            jsonObj = new JSONObject(messages[0].getData());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            contacts = jsonObj.getJSONArray("messages");
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);
                notify += c.getString("name") + "\t";
                notify += c.getString("type") + "\t";
                notify += c.getString("price") + "\t";
                if (c.getString("status").equals("1")) {
                    notify += "\u25b2 \t";
                } else {
                    notify += "\u25bc \t";
                }
                notify += "\n";
                for (int j = 0; j < MainActivity.contactList.size(); j++) {
                    if (MainActivity.contactList.get(j).get("sourceName").equals(c.getString("name"))
                            && MainActivity.contactList.get(j).get("assetName").equals(c.getString("type"))) {
                        MainActivity.contactList.get(j).put("priceCurrency", c.getString("price"));
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // looping through All Contacts

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(notify))
                .setSmallIcon(R.mipmap.ic_launcher) //replace with your app icon if it is not correct
                .setTicker("پیام از بفرست!")
                .setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

0 个答案:

没有答案