如何更新已打开的导航抽屉?

时间:2015-01-14 15:53:04

标签: android performance navigation-drawer android-adapter notifydatasetchanged

我已经在互联网上搜索了这个主题,并没有找到解决方案。

我使用导航抽屉作为"收件箱"用户可以看到他的聊天记录。 NotifyDataSetChanged适用于" onDrawerOpened"处理程序,但我想更新ND,即使它处于打开状态。

我试过: 使listiew无效, 使DrawerLayout无效, 清除并添加到适配器的集合我称为notifydatasetchanged

是否有任何选项可以在onDrawerOpen,Close,Slide等方法之外更新ND?

这是我的代码onCreate方法:

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
    DatabaseHandler db=new DatabaseHandler(MainNavActivity.this);
    listMsg=db.getLastMsg();
    mDrawerList = (ListView) findViewById(R.id.drawer_list);
    adapter=new NavDrawerListAdapter(this,listMsg);
    // Setting the adapter on mDrawerList
    mDrawerList.setAdapter(adapter);
    mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerOpened(View drawerView) {
            DatabaseHandler db=new DatabaseHandler(MainNavActivity.this);
            listMsg.clear();
            listMsg.addAll(db.getLastMsg());
            adapter.notifyDataSetChanged();
        }
    });

我的适配器:

    public class NavDrawerListAdapter extends ArrayAdapter<Message> {
    public NavDrawerListAdapter(Context context, ArrayList<Message> msg) {
        super(context, 0, msg);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Message msg=getItem(position);
        Log.v("onReceieve","getView");
        if (convertView == null) {
            holder= new ViewHolder();
            LayoutInflater mInflater = (LayoutInflater)
                    getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.navigation_drawer_list_item, null);
            holder.name=(TextView)convertView.findViewById(R.id.textViewName);
            holder.msg=(TextView)convertView.findViewById(R.id.textViewMsg);
            holder.time=(TextView)convertView.findViewById(R.id.textViewTime);
            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }

            holder.msg.setText(msg.getText());
            holder.time.setText(msg.getTime());
            holder.name.setText(msg.getName());

        return convertView;
    }
}

在这里,我收到新消息并尝试更新ND

    private BroadcastReceiver onNotice= new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("onReceive","Poklican");

        DatabaseHandler db=new DatabaseHandler(MainNavActivity.this);
        listMsg.clear();
        listMsg.addAll(db.getLastMsg());
        adapter.notifyDataSetChanged();
    }
};

1 个答案:

答案 0 :(得分:0)

您未通过适配器进行数据更改。但是,它不会自动更新显示。在要更改的数据的主机上使用.notifyDataAetChanged()。