How to pass a data from Empty Activity to Tabbed Activity?

554 views Asked by At

I just want to pass a data from my Empty Activity to Tabbed Activity..In my code i am using three Activities.One is Main Activity which contains List Views..Second is Empty Activity..third is Tabbed Activity..In my Main Activity,User can click an Item from listview...When user clicks Item,item passed to second activity..it works perfectly..now i want to pass a item name from MainActivity to TabbedActivity(DesAndCode - Activty Name)..Here i also include my code..please look it up..

DesAndCode - TabbedActivity

package ajay.com.mydict;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;
import android.widget.Toast;

public class DesAndCode extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    static String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_des_and_code);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
         //get Intent value from ListActi.java 
         s = getIntent().getStringExtra("ClickedItem");   
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
   if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";
        public PlaceholderFragment() {
        }
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // 1 -- > Description
            if(getArguments().getInt(ARG_SECTION_NUMBER)==1){
                View rootView = inflater.inflate(R.layout.fragment_code_page, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.code_page);
               //s has value which receives from ListActi 
                if(s.equals("Access Modifier")){
                    textView.setText(R.string.blackbox);
                }
                return rootView;
            }
            else { // Sample Code
                View rootView = inflater.inflate(R.layout.fragment_des_and_code, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.section_label);
                textView.setText(R.string.blackbox);
                return rootView;
            }
        }
    }
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "DESCRIPTION";
                case 1:
                    return "SAMPLE CODE";
            }
            return null;
        }
    }
}

ListActi.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        lv = (ListView) findViewById(R.id.listView);
        final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(listElements));
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getApplicationContext(), android.R.layout.simple_list_item_1, ListElementsArrayList);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getApplicationContext(), listElements[position],Toast.LENGTH_LONG).show();
                if(position == 1){
                    Intent i = new Intent(getApplicationContext(),DesAndCode.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 0){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 2){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 3){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 4){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 5){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 6){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 7){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }    if(position == 8){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 9){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 10){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
                if(position == 11){
                    Intent i = new Intent(getApplicationContext(),OnlyDes.class);
                    i.putExtra("ClickedItem", listElements[position]);
                    startActivity(i);
                }
            }
        });
    }
}
0

There are 0 answers