使用adapter.notifyDataSetChanged()更新RecyclerView

时间:2015-08-11 20:57:56

标签: java android broadcastreceiver countdowntimer android-recyclerview

我已经实施了一个RecyclerView,它有一个倒计时器的用户界面。我创建了一个BroadcastService类,它创建了一个CountDownTimer,并将onTick()方法中的计时器内容广播到我的MainActivity,在那里我使用BroadCast接收器来更新UI。

我的BroadcastReceiver只接收来自BroadcastService的初始值。我认为这是因为我没有通知回收商视图的适配器数据已经改变。但是,由于范围可变,我无法从广播接收器访问我的适配器。

也许我对变量范围基本缺乏了解,但我如何从

访问适配器
            adapter = new DataAdapter(getApplicationContext(), data);

在我的广播接收机课程中?因为现在它没有得到承认。

这是我的班级定义+ onCreate()

public class Profile_Page extends  ActionBarActivity implements DataAdapter.ClickListener {

    private RecyclerView recyclerView;
    public DataAdapter adapter;
    private Context context;
    String currentUser;
    Data current = new Data();

    final List<Data> data = new ArrayList<>();
    public static String BROADCAST_ACTION =
            "packagename.countdown_br";

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(br, filter);

        startService(new Intent(this, Broadcast_Service.class));
        setContentView(R.layout.activity_profile__page);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("ParseClass");
        query.whereEqualTo("author", ParseUser.getCurrentUser());
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override

            public void done(List<ParseObject> list, ParseException e) { 
                if (e == null) {
                    for (ParseObject getData : list)
                    {
                        current.1= getData.getString("1");
                        current.2= getData.getString("2");
                        current.3= getData.getString("3");
                        current.4= getData.getString("4");
                        current.5= getData.getString("5");


                        data.add(current);

                    }
                }
                else {

                     }


                adapter = new DataAdapter(getApplicationContext(), data);

                recyclerView.setAdapter(adapter); //set recyclerView to this adapter


            }



        });


    }

这是我的广播接收器代码[也在MainActivity.java中]

public BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            updateGUI(intent);
            //HOW TO NOTIFY DATA SET CHANGE

        }
    };

public void updateGUI(Intent intent) {
        if (intent.getExtras() != null) {
            long millisUntilFinished = intent.getLongExtra("countdown", 0);
            current.goalTimer = String.valueOf(intent.getExtras().getLong("countdown") / 1000);

        }


    }

而且,如果它有用,请点击我的广播服务课程:

public class Broadcast_Service extends Service {
    private final static String TAG = "BroadcastService";
    LocalBroadcastManager broadcastManager;
    public static final String COUNTDOWN_BR = "packagename.countdown_br";
    Intent bi = new Intent(COUNTDOWN_BR);

    CountDownTimer cdt = null;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Starting timer...");

        cdt = new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                bi.putExtra("countdown", millisUntilFinished);
                sendBroadcast(bi);
            }

            @Override
            public void onFinish() {
                Log.i(TAG, "Timer finished");
            }
        };

        cdt.start();
    }

    @Override
    public void onDestroy() {
        cdt.cancel();
        Log.i(TAG, "Timer cancelled");
        super.onDestroy();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,按如下方式扩展BroadcastReciever类:

public class MyReciever extends BroadcastReciever{
    private Profile_Page activity;

    public MyReciever(Profile_Page activity){
        this.activity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        activity.updateGUI(intent);
    }
}

创建活动的静态实例并将其传递给接收者。

public class Profile_Page extends  ActionBarActivity implements DataAdapter.ClickListener {
    private static Profile_Page instance;
    private MyReciever myReceiver;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        myReceiver = new MyReciever(instance);
        ...
    }

    public void updateGUI(Intent intent) {
        ...
    }
}

现在您可以非常轻松地访问适配器。希望这会有所帮助。