【问题标题】:How to connect fragment list Activity with Button click Main Activity?如何将片段列表活动与按钮单击主活动连接起来?
【发布时间】:2014-12-23 07:36:33
【问题描述】:
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.title = (String) getTitle();
    // Getting reference to the DrawerLayout
    this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    this.drawerList = (ListView) findViewById(R.id.drawer_list);

    this.fragments[0] = new My_Main_Fragment(this);
    this.fragments[1] = new My_Breakfast_Fragment();
    this.fragments[2] = new My_Lunch_Fragment();
    this.fragments[3] = new My_Dinner_Fragment();
    this.fragments[4] = new My_Dessert_Fragment();
    this.fragments[5] = new My_Seat_Plan_Fragment();
    this.fragments[6] = new My_Beverages_Fragment();

    this.item_titles = getResources().getStringArray(R.array.menu_items);

.................................................. ..................................................... ..................................................... ..................................................... …………

public class My_Seat_Plan_Fragment extends Activity{

按钮创建、编辑、查看、删除、查找;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.air_main_interface);

    create = (Button)findViewById(R.id.note_add);
    edit = (Button)findViewById(R.id.note_edit);
    view = (Button)findViewById(R.id.note_view);
    delet = (Button)findViewById(R.id.note_delete);
    find = (Button)findViewById(R.id.note_find);

    create.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent in = new Intent(My_Seat_Plan_Fragment.this,Seat_create.class);
            startActivity(in);
        }
    });

.................................................. ..................................................

在下面附加的两个类中,我需要将 Main Activity 与片段类连接起来...... Main Activity 跟随着 extends Activity 实现 My_Fragments_Parent {

和 Fragment Activity 如下公共类 My_Seat_Plan_Fragment extends Activity{

但我在该行的主要活动中遇到错误

this.fragments[5] = new My_Seat_Plan_Fragment(); ------ 说不能从 My Seat_Plan_Fragment 转换为 Fragment

因此,如果我更改 My seat_Plan Fragment Extends Activity,则无法完成按钮活动...

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    如果我理解了这个问题,您可以在片段中使用类似这样的内容:

    ((MainActivity) getActivity()).someMethod();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      相关资源
      最近更新 更多