当用户点击菜单操作栏中的刷新图标时,我想刷新片段的内容。
我的应用程序在一个活动中有三个片段与视图寻呼机;我尝试通过在onOptionsItemSelected()
中调用它们来刷新所有这些,并且我对它们执行了事务,当用户单击刷新菜单时应用程序崩溃。
我读过这个问题,它可能与我的相似,但我找不到合适的答案来解决我的问题:android: menu item click event from fragment我也读过这篇文章:但没有任何帮助我:{{3也许我没有以正确的方式做到这一点。
我在活动中刷新所有三个片段的代码如下:
@Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
switch (item.getItemId()){
case R.id.refresher_id:
fragmentTransaction.detach(sentMsg).attach(sentMsg).commit();
fragmentTransaction.detach(receivedMsg).attach(receivedMsg).commit();
fragmentTransaction.detach(allMsg).attach(allMsg).commit();
break;
}
return super.onOptionsItemSelected(item);
}
这些是一个片段的代码:
public class Page2_sent_msg extends Fragment {
//default constructor
public Page2_sent_msg(){}
@SuppressLint("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
final View Page2_sent_msg=inflater.inflate(R.layout.page2_sent_msg,container,false);
ListView sentMsgListView=(ListView)Page2_sent_msg.findViewById(R.id.sentMsgListview);
ArrayList<String> sentMsgArrayList=new ArrayList<String>();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
}
ArrayAdapter<String>sentMsgAdapter=new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1,sentMsgArrayList);
sentMsgListView.setAdapter(sentMsgAdapter);
sentMsgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//this is what will happen when a user clicks one item from the lis view
}
});
Page2_sent_msg.setTag("sentMsg");
return Page2_sent_msg;
}
我真的需要帮助。亲切的问候!
答案 0 :(得分:0)
在片段中添加方法刷新
public void refreshFragment() {
sentMsgArrayList.clear();
sentMsgAdapter.notifyDataSetChanged();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
sentMsgAdapter.notifyDataSetChanged
}
一旦用户点击刷新
,就调用它@Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
switch (item.getItemId()){
case R.id.refresher_id:
sentMsg.refreshFragment();
receivedMsg.refreshFragment();
allMsg.refreshFragment();
break;
}
return super.onOptionsItemSelected(item);
}