如何从另一个活动中调用不同活动的功能

时间:2016-03-11 16:45:49

标签: android

我想从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中(即刷新我的列表视图)。现在它显示我关闭然后启动我的应用程序后输入的新数据。

1 个答案:

答案 0 :(得分:0)

<fragment>标记内的xml文件中添加此内容。

  android:tag="any_tag"

获取Fragment的当前对象在java文件中执行此操作(在TimePickerPopup.java中添加此onClick(View v)方法)

MainActivityFragment2 ma2 =  (MainActivityFragment2) getFragmentManager().findFragmentByTag("any_tag");
ma2.populateTimeList();