0

我已經通過互聯網搜索了這個話題,並沒有找到解決辦法。如何更新已打開的導航抽屜?

我使用導航抽屜作爲「收件箱」,用戶可以在其中看到他的聊天記錄。 NotifyDataSetChanged與「onDrawerOpened」處理程序正常工作,但我想要更新ND,即使它處於打開狀態。

我想: 無效listiew, 無效DrawerLayout, 清算和增加集合ofcourse適配器我叫notifydatasetchanged

是否有任何選項onDrawerOpen,關閉外更新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(); 
    } 
}; 

回答

0

您notfied適配器用於數據更改。但是,它不會自動更新顯示。在正在更改的數據的主機上使用.notifyDataAetChanged()。

+0

你的意思是listview適配器?我正在用adapter.notifyDataSetChanged()做這件事,這段代碼在onOpen()方法中工作,但不在我的接收器中。 – gregaj