在 Resume 上更新列表视图(无法获取视图)
update List View onResume (can't get view)
我需要在 onResume 函数中更新我的列表视图,他们的问题是我的视图在那里是空的:
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();
}
}
知道如何更新它吗?
谢谢!
伊兰.
@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 中注销您的广播接收器,否则它可能会以意想不到的方式接收通知..
如果您使用 Activity 或 FragmentActivity[,请更改您的 OnCreate 函数=20=]:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.large_screen_layout);
mList = (ListView)findViewById(R.id.list);
}
或者如果你想使用 Fragment
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;
}
试试这个:
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();
}
}
我需要在 onResume 函数中更新我的列表视图,他们的问题是我的视图在那里是空的:
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();
}
}
知道如何更新它吗?
谢谢!
伊兰.
@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 中注销您的广播接收器,否则它可能会以意想不到的方式接收通知..
如果您使用 Activity 或 FragmentActivity[,请更改您的 OnCreate 函数=20=]:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.large_screen_layout);
mList = (ListView)findViewById(R.id.list);
}
或者如果你想使用 Fragment
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;
}
试试这个:
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();
}
}