-1

I have a listview in viewpager. I want to start a new activity or if that possible, to make same activity for multiple xml, if I click on item in that list view.

But my class does not extend activity.

This is my code:

    public class SampleListFragment extends ScrollTabHolderFragment implements OnScrollListener {
    private static final String ARG_POSITION = "position";
    private ListView mListView;
    private ArrayList<String> mListItems;
    private int mPosition;

    public static Fragment newInstance(int position) {
    SampleListFragment f = new SampleListFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f; }
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPosition = getArguments().getInt(ARG_POSITION);
    mListItems = new ArrayList<String>();
    if (mPosition==0){
       mListItems.add("Game" );
       mListItems.add(".iphone " );
       mListItems.add("xxx" );
       mListItems.add("yyy" );
       mListView.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {
          switch (position){
             case 0 : // i want to start new activity if i clicked in game 
                break;
             case 1 : //start a new activity if i clicked in iphone 
                break;
          }
        }
        private Context getApplicationContext() {
            // TODO Auto-generated method stub
            return null;
        }
     });
}   else if (mPosition==1){
   mListItems.add(". item - currnet page:حة 1 " );
}   else if (mPosition==2){
        mListItems.add(". item - currnet page:حة2  " );
}  else if (mPosition==3){
   mListItems.add(". item - currnet page:حة 3 " );
}  else if (mPosition==4){
   mListItems.add(". item - currnet page:حة 4 " );}}
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle        savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_list, null);
      mListView = (ListView) v.findViewById(R.id.listView);
      View placeHolderView = inflater.inflate(R.layout.view_header_placeholder, mListView, false);
      mListView.addHeaderView(placeHolderView);
      return v; 
   }
   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      mListView.setOnScrollListener(this);
      mListView.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item,       android.R.id.text1, mListItems));}
      @Override
      public void adjustScroll(int scrollHeight) {
         if (scrollHeight == 0 && mListView.getFirstVisiblePosition() >= 1) {
         return;
      }
      mListView.setSelectionFromTop(1, scrollHeight);}
      @Override
       public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
          if (mScrollTabHolder != null)
             mScrollTabHolder.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount, mPosition);
          }
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
             // nothing  
       }
    }
aliteralmind
  • 19,847
  • 17
  • 77
  • 108

2 Answers2

0

You can call

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

when you want to start a new Activity from your Fragment

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • Maybe you should add flag into intent about new task. Try it. – Aleksandr Jul 12 '14 at 12:27
  • 2
    What is the problem? `its not working` is hard to understand. Does it crash? Nothing happens? – Apoorv Jul 12 '14 at 12:30
  • it crash , how to add flag into intent ? can you explain I'm still beginner in android :( – user3080462 Jul 12 '14 at 12:42
  • yes i do , is this statements in my code is true ?? mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { switch (position){ – user3080462 Jul 12 '14 at 12:47
0

It's possibly because non activity class doesn't have context of their own.

So you need context to start another activity .

this code should work for you

    Intent intent = new Intent(getApplicationContext(),yourActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplicationContext.startactivity(intent);
Aleksandr
  • 787
  • 6
  • 22
someone
  • 427
  • 2
  • 11
  • what is call meaning ? should i have to identify getApplicationContext () method ? or its already exist ? – user3080462 Jul 12 '14 at 13:09
  • I copied my code and forget to change that,it is name of your intent.It will be already there,if not get context from your activity class and use it – someone Jul 12 '14 at 13:12
  • i cant do getApplicaionContext method ? because i have this SampleListFragment f = new SampleListFragment(); – user3080462 Jul 12 '14 at 14:06
  • get the context from activity you want to start.Refer [this](http://stackoverflow.com/questions/17917968/get-context-in-non-activity-class-android) to know how to get context from other activity – someone Jul 12 '14 at 14:08