我需要在onResume函数中更新我的List View,问题是我的视图在那里是null:
mList = (ListView) v.findViewById(R.id.list); //v is null
我该怎么办?如何获取列表视图以进行更新?
以下是onCreate函数的一部分:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = findViewById(R.id.large_screen_layout);
mList = (ListView) v.findViewById(R.id.list);
}
以下是恢复功能:
@Override
protected void onResume()
{
super.onResume();
mMyBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
// Here you can refresh your listview or other UI
String textmessage = intent.getStringExtra("Message");
Toast.makeText(getApplicationContext(), textmessage, Toast.LENGTH_LONG).show();
item = new RowItem(R.drawable.alert_icon, "text1", "text2");
rowItems.add(item);
adapter = new CustomListViewAdapter(getApplicationContext(),R.id.list,rowItems);
adapter.notifyDataSetChanged();
//now either, i need to use view (v) or mList(List View):
mList = (ListView) v.findViewById(R.id.list); //v and mList are NULL here
mList.setAdapter(adapter);
}
};
try {
LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver,new IntentFilter("your_action"));
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
任何想法如何更新它?
谢谢!
叶兰。
答案 0 :(得分:0)
@Override
protected void onResume()
{
super.onResume();
mMyBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
// Here you can refresh your listview or other UI
String textmessage = intent.getStringExtra("Message");
Toast.makeText(getApplicationContext(), textmessage, Toast.LENGTH_LONG).show();
item = new RowItem(R.drawable.alert_icon, "text1", "text2");
rowItems.add(item);
adapter = new CustomListViewAdapter(getApplicationContext(),R.id.list,rowItems);
adapter.notifyDataSetChanged();
//now either, i need to use view (v) or mList(List View):
// need to find V
View v = findViewById(R.id.large_screen_layout);
mList = (ListView) v.findViewById(R.id.list); //v and mList are NULL here
mList.setAdapter(adapter);
}
};
try {
LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver,new IntentFilter("your_action"));
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
还要确保在onPause中取消注册广播接收器,否则它可能会以意想不到的方式接收通知。
答案 1 :(得分:0)
如果您使用活动或 FragmentActivity ,请更改 OnCreate 功能:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.large_screen_layout);
mList = (ListView)findViewById(R.id.list);
}
或者如果您想使用片段
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.large_screen_layout, null);
mList = (ListView)view.findViewById(R.id.list);
return view;
}
答案 2 :(得分:0)
试试这个:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = findViewById(R.id.large_screen_layout);
mList = (ListView) v.findViewById(R.id.list);
adapter = new CustomListViewAdapter(getApplicationContext(),
R.id.list,rowItems);
mList.setAdapter(adapter);}
@Override
protected void onResume() {
super.onResume();
mMyBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String textmessage = intent.getStringExtra("Message");
Toast.makeText(getApplicationContext(), textmessage, Toast.LENGTH_LONG).show();
item = new RowItem(R.drawable.alert_icon, "text1", "text2");
rowItems.add(item);
adapter.notifyDataSetChanged();
}
};
try {
LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver, new IntentFilter("your_action"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}