我想從TimePickerPopup類的onClick函數中調用MainFragmentActivity2類中的函數populateTimeList()。我該怎麼做?這些是分別的代碼。我對android非常陌生,請幫忙!如何從另一個活動調用不同活動的函數
public class MainActivityFragment2 extends Fragment {
private List<TimeList> TDList = new ArrayList<TimeList>();
public MainActivityFragment2() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void populateTimeList() {
Context ctx = this.getActivity();
DBoperations db = new DBoperations(ctx);
Cursor cr = db.getInformation(db);
cr.moveToFirst();
String TimeH, TimeM, EtimeH, EtimeM;
do {
TimeH = Integer.toString(cr.getInt(1));
TimeM = Integer.toString(cr.getInt(2));
EtimeH = Integer.toString(cr.getInt(3));
EtimeM = Integer.toString(cr.getInt(4));
TDList.add((new TimeList(TimeH + " : " + TimeM, EtimeH + " : " + EtimeM)));
}while (cr.moveToNext());
}
private class MyAdapter extends ArrayAdapter<TimeList> {
public MyAdapter(Context context, List<TimeList> values) {
super(context, R.layout.time_list_item,values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = LayoutInflater.from(getContext());
if(v == null){
v = inflater.inflate(R.layout.time_list_item, parent, false);
}
TimeList currLoc = TDList.get(position);
TextView theTextView = (TextView) v.findViewById(R.id.time_list_item_textview1);
theTextView.setText(currLoc.getTime());
theTextView = (TextView) v.findViewById(R.id.time_list_item_textview2);
theTextView.setText(currLoc.getDuration());
return v;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.time_list_view, container, false);
ListView list = (ListView) rootView.findViewById(R.id.time_list_view);
final ArrayAdapter<TimeList> adapter = new MyAdapter(getActivity(),TDList);
list.setAdapter(adapter);
populateTimeList();
setRetainInstance(true);
ImageButton ib = (ImageButton) rootView.findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivityFragment2.this.getActivity(), TimePickerPopup.class));
}
});
return rootView;
}
}
而且
public class TimePickerPopup extends Activity {
private List<TimeList> TDList = new ArrayList<TimeList>();
Context ctx =this;
private TimePicker time_picker1;
private TimePicker time_picker2;
private ImageButton select_time1;
private ImageButton select_time2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_picker);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .7+50));
time_picker1 = (TimePicker) findViewById(R.id.timePicker1);
time_picker2 = (TimePicker) findViewById(R.id.timePicker2);
select_time1 = (ImageButton) findViewById(R.id.selectButton1);
select_time2 = (ImageButton) findViewById(R.id.selectButton2);
select_time1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Start time " + time_picker1.getCurrentHour() + " : " + time_picker1.getCurrentMinute(), Toast.LENGTH_SHORT).show();
ScrollView sv1 = (ScrollView) findViewById(R.id.sv);
sv1.fullScroll(View.FOCUS_DOWN);
}
});
select_time2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"End time " + time_picker2.getCurrentHour() + " : " + time_picker2.getCurrentMinute(), Toast.LENGTH_SHORT).show();
DBoperations db = new DBoperations(ctx);
db.putInfo(db, time_picker1.getCurrentHour(), time_picker1.getCurrentMinute(), time_picker2.getCurrentHour(), time_picker2.getCurrentMinute(), 0, 0);
finish();
}
});
}
}
基本上,我想從TimePickerPopup類數據庫剛剛輸入的數據在ListView露面(即刷新我的列表視圖)。目前它顯示了我關閉然後啓動我的應用程序後輸入的新數據。
爲什麼不使用全局方法(單身)? –
我不確定這是否是將數據傳遞給其他活動的副本。這裏的問題是代碼是緊密耦合的,所以它必須被重構。 – hehe
我該怎麼做? – PKBEST