我在一個Activity中有兩個片段。 第一個片段:用戶選擇日期和時間,然後按「選擇」按鈕。 用戶發送到SQL DB的選擇。用戶可以選擇他想要的日期和時間。 第二個片段:通過從SQL DB中讀取,在ListView中設置所有用戶的選擇(日期和時間)。如何使用兩個片段中的一個按鈕
我的問題:我不是每次都能成功刷新列表視圖,用戶按在片段「選擇」按鈕1.
第一塊碎片
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();
}
}
After lon g搜索,有什麼幫助?謝謝!