简体   繁体   English

更新列表视图onResume(无法查看)

[英]update List View onResume (can't get view)

I need to update my List View in onResume function, them problem is that my view is null there: 我需要在onResume函数中更新列表视图,它们的问题是那里的视图为null:

mList = (ListView) v.findViewById(R.id.list); //v is null

what should I do? 我该怎么办? how can I get my list view to update it? 如何获取列表视图进行更新?

Here is part of onCreate function: 这是onCreate函数的一部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = findViewById(R.id.large_screen_layout);
    mList = (ListView) v.findViewById(R.id.list);
}

Here is the on resume function: 这是恢复时的功能:

@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();
}
}

any idea how can I update it? 知道如何更新吗?

Thank you! 谢谢!

Eran. 伊朗

@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();
}
}

Also be sure to deregister your broadcast receiver in onPause or it may receive notifications in unexpected ways.. 另外,请务必在onPause上注销广播接收器的注册,否则它可能会以意外方式接收通知。

Change your OnCreate function if you use Activity or FragmentActivity : 如果使用ActivityFragmentActivity,请更改OnCreate函数:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.large_screen_layout);    
    mList = (ListView)findViewById(R.id.list);
}

Or if u want to use 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;
}

Try this: 尝试这个:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM