如何从片段中调用Activity

时间:2013-12-25 20:36:27

标签: android android-fragments android-activity

我有Pageradapter,其中有3个片段。

我在第一个片段上有按钮

我想要实现的是已安装的应用程序的打开列表,带有复选框。

我创建了一个显示带有复选框的ListView的活动

我无法打开那项活动。

MainActivity

package com.aditya.att;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {



    // Tab titles
    //private String[] tabs = { "Setting", "Charts", "Usage", "Add" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         /** Getting a reference to the ViewPager defined the layout file */        
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating PagerAdapter */
        PagerAdapter pAdapter = new PagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        viewPager.setAdapter(pAdapter);

        /** Called when the user clicks the Send button */


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


fragment AppSetting

package com.aditya.att;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;



public class AppSetting extends Fragment {


    public void sendMessage(View view) {
      Intent intent = new Intent(getActivity(), MyList.class);

        startActivity(intent);
      //getActivity().startActivity(intent);


    }    
}

活动MyList

package com.aditya.att;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
 import android.widget.ArrayAdapter;

public class MyList extends ListActivity {


/** Called when the activity is first created. */

  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // create an array of Strings, that will be put to our ListActivity
    ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
        getModel());
    setListAdapter(adapter);
  }



  private List<Model> getModel() {
    List<Model> list = new ArrayList<Model>();
    list.add(get("Linux"));
    list.add(get("Windows7"));
    list.add(get("Suse"));
    list.add(get("Eclipse"));
    list.add(get("Ubuntu"));
    list.add(get("Solaris"));
    list.add(get("Android"));
    list.add(get("iPhone"));

    // Initially select one of the items
    list.get(1).setSelected(true);
    return list;
  }

  private Model get(String s) {
    return new Model(s);
  }

}

适配器InteractiveArrayAdapter

package com.aditya.att;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

  private final List<Model> list;
  private final Activity context;

  public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
  }

  static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.rowbuttonlayout, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.label);
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
      viewHolder.checkbox
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              Model element = (Model) viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });
      view.setTag(viewHolder);
      viewHolder.checkbox.setTag(list.get(position));
    } else {
      view = convertView;
      ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    return view;
  }
} 

班级模型

package com.aditya.att;

public class Model {

  private String name;
  private boolean selected;

  public Model(String name) {
    this.name = name;
    selected = false;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public boolean isSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    this.selected = selected;
  }

} 

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_tab_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </android.support.v4.view.ViewPager>

</RelativeLayout>

fragment_setting

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#5cdf06">

  <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Setting add Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>

  <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="66dp"
      android:layout_marginTop="26dp"
      android:drawableBottom="@drawable/btn_toggle_off"
      android:text="Button" 
      android:onClick="sendMessage"/>

</RelativeLayout>

rowbuttonlayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30px" >
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px" >
    </CheckBox>

</RelativeLayout> 

PagerAdapter

package com.aditya.att;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class PagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 4;
    public  Bundle data;

    /** Constructor of the class */
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
        case 0:         
        AppSetting myFragment = new AppSetting();
        /*data= new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);*/
        return myFragment;
        case 1:
            AppActivity myFragment1 = new AppActivity();
            /*data = new Bundle();
            data.putInt("current_page", arg0+1);
            myFragment1.setArguments(data);*/
            return myFragment1;
        }
        AppSetting myFragment2 = new AppSetting();
        /*data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment2.setArguments(data);*/
        return myFragment2;
    }



    /** Returns the number of pages */
    @Override
    public int getCount() {     
        return PAGE_COUNT;
    }
    @Override
    public CharSequence getPageTitle(int position) {    
        switch (position) {
        case 0:
            // Top Rated fragment activity
            return "Setting";
        case 1:
            // Games fragment activity
            return "Usage";
        case 2:
            // Movies fragment activity
            return "Actitvity";
        case 3:
            // Add fragment activity
            return "Add";
        }

        return null;

    }

}

还有两个碎片 1-AppActivity 1-AppUsage 但是它们还没有代码

我希望我已经清除了一切。

使用pagertitlestrip加载mainactivity ---&gt;关于设置---&gt;点击按钮---&gt;使用复选框打开已安装应用程序的活动列表。(使用ListView作为占位符)

应用锁定中提供的确切功能,要求用户选择要锁定的应用。

谢谢

1 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(getActivity(), Activity_name.class);
                 startActivity(intent);