我在一个Activity中有2个片段。 第一个片段:用户选择日期和时间,然后按“选择”按钮。 选择发送到SQL DB的用户。用户可以根据需要选择日期和时间。 第二个片段:通过从SQL DB中读取,在ListView中设置所有用户选择(日期和时间)。
我的问题:每次用户按下片段1中的“选择”按钮时,我都无法刷新listView。
First Fragment
public class ChooseTimeFragment extends Fragment {
String date,time,amPm;
Spinner spinnerDate,spinnerTime,spinnerAmPm;
private DbHelper db;
public ChooseTimeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view=inflater.inflate(R.layout.fragment_choose_time, container, false);
db = DbHelper.getInstance(view.getContext());
spinnerDate = (Spinner) view.findViewById(R.id.spinnerDate);
ArrayAdapter<CharSequence> adapterDate = ArrayAdapter.createFromResource(getActivity(),
R.array.date_array, android.R.layout.simple_spinner_item);
adapterDate.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDate.setAdapter(adapterDate);
date=spinnerDate.getSelectedItem().toString();
spinnerTime = (Spinner) view.findViewById(R.id.spinnerTime);
ArrayAdapter<CharSequence> adapterTime = ArrayAdapter.createFromResource(getActivity(),
R.array.time_array, android.R.layout.simple_spinner_item);
adapterTime.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTime.setAdapter(adapterTime);
time=spinnerTime.getSelectedItem().toString();
spinnerAmPm = (Spinner) view.findViewById(R.id.spinnerAmPm);
ArrayAdapter<CharSequence> adapterAmpm = ArrayAdapter.createFromResource(getActivity(),
R.array.amPm_array, android.R.layout.simple_spinner_item);
adapterAmpm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAmPm.setAdapter(adapterAmpm);
amPm=spinnerAmPm.getSelectedItem().toString();
Button bChoose=(Button) view.findViewById(R.id.btnChooseDT);
bChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initialize();
if(validate()){
db.addUserChooseDateAndTime(date,time+amPm,view.getContext());
}
}
});
return view;
}
private void initialize(){
date=spinnerDate.getSelectedItem().toString();
time=spinnerTime.getSelectedItem().toString();
amPm=spinnerAmPm.getSelectedItem().toString();
}
private boolean validate(){
boolean valid=true;
if (date.equals("DATE")||time.equals("TIME")){
Toast.makeText(getContext(),date + " Please Enter Date and Time", Toast.LENGTH_SHORT).show();
valid=false;
}
return valid;
}
}
第二片段
public class ChosenListFragment extends Fragment {
private DbHelper db;
public static Activity myActivity;
public ChosenListFragment() {
}
MyChosenListViewAdapter adapter;
ListView listView;
ArrayList<TimeAndDate> timeAndDateArrayLis;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_chosen_list, container, false);
db = DbHelper.getInstance(view.getContext());
refreshList(view);
return view;
}
public void refreshList(View view){
ArrayList<TimeAndDate> timeAndDateArrayList =db.getListDateAndTime(getContext());
adapter = new MyChosenListViewAdapter(getContext(), timeAndDateArrayList);
listView = (ListView) view.findViewById(R.id.listDateAndTime);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
经过长时间的搜索,有什么帮助吗?谢谢!
答案 0 :(得分:0)
问题是,每次单击按钮时,您都没有刷新listView。每次点击后都应刷新清单。如果你也可以分享你的主动课程,我可以仔细看看。
答案 1 :(得分:0)
简单方法:
如果您在一个活动中有两个片段(例如:ParentActivity),您可以通过父活动从一个片段访问另一个片段。
将这样的内容添加到父活动:
public void refreshListFragment(){
ListFragment list =
getSupportFragmentManager().findFragmentById(R.id.fragment_list);//init on onCreate or ... is better.
list.refreshList();
}
在db.addUserChooseDateAndTime(date,time + amPm,view.getContext())之后添加以下代码;
((ParentActivity)getActivity()).refreshListFragment();
更好的方式:
使用新的Android架构组件link
或
使用界面来传达Android official site
或
使用观察者模式通知已更改。
LocalBroadcastManager:
public class ListFragment extends Fragment {
public static final String ACTION_UPDATE = "action_update";
private BroadcastReceiver updateBroadcastListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Updated", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(getContext())
.registerReceiver(updateBroadcastListener, new IntentFilter(ListFragment.ACTION_UPDATE));
}
@Override
public void onPause() {
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(updateBroadcastListener);
super.onPause();
}
....
}
并且在对列表项进行任何更改之后:
LocalBroadcastManager.getInstance(SecondActivity.this).sendBroadcast(new Intent(ListFragment.ACTION_UPDATE));
你可以编写自己的观察者模式或使用像EventBus这样的东西 http://greenrobot.org/eventbus/documentation/how-to-get-started/
您还可以注册到数据库游标,以便通知任何更新。 Listen to any changes in a cursor in android