Search code examples
androidandroid-intentandroid-fragmentsandroid-fragmentactivity

Converting Intent to Fragment in Android


In general most of the time I work with Intent rather than Fragment because I found It somehow complex. I have a class with ListView to show some data from an array and I want to convert it in Fragment also want to pass the extras in a new fragmented class.

ListView Class:

public class MainActivity extends Activity {
    ListView list;
    String[] web = { "Google", "Twitter", "Windows", "Bing", "Itunes",
            "Wordpress", "Drupal" };
    Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "You Clicked at " + web[+position], Toast.LENGTH_SHORT)
                        .show();


                Intent i = new Intent(MainActivity.this, intent.class);
                i.putExtra("name", web[+position]);
                startActivity(i);

            }
        });

    }

}

CustomList Class:

public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    public CustomList(Activity context, String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

I got a main class with three tabs created using fragment and in this class I would like to add the ListView!

public class LayoutOne extends Fragment {


    public static Fragment newInstance(Context context) {
        LayoutOne f = new LayoutOne();  

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
        return root;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

}

What I want to do is to show the listview in the fragment class and when a list item will be click I want to launch the New Intent to show which Item was clicked!


Solution

  • If you want to pass parameters into a Fragment, common practice is to create a static method that produces an instance of your Fragment with certain parameters. You already have the newInstance method, now you just need to add parameters to the arguments and return the Fragment. You should check for the arguments in your onCreateView The following is an example of how you would write one.

    public class LayoutOne extends Fragment 
    {
    
        public static Fragment newInstance(int someInt, String someString) 
        {
            LayoutOne f = new LayoutOne();
    
            Bundle args = new Bundle();
            args.putInt("someInt", someInt);
            args.putString("someString", someString);
            f.setArguments(args);
    
            return f;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {
            ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
            Bundle args = getArguments();
    
            //From here you would check to see if your arguments are present 
            //proceed accordingly
    
            return root;
    
        }
    }
    

    For your specific case, you could pass along the String array and add it as an argument.

    Hope this helps. Good luck!

    Edit

    The problem you're facing is that you're instantiating a ListView in your Activity when in reality you should be doing it in your Fragment. Intents aren't needed here, you just need to make sure that there is a ListView attribute to get from the layout you attach to your Fragment. The following is a small example of how that should work.

    Note, this method goes in your LayoutOne class

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
    
        //Be sure to use the actual id of your list view
        ListView lv = (ListView) root.findViewById(R.id.list_view_id);
    
        //Make sure that you retrieve the values for 'web' and 'imageId' from 
        //the activity using the "newInstance" method.
        CustomList adapter = new CustomList(getActivity(), web, imageId);
        lv.setAdapter(adapter);
    
        //From here, set your onClickListener and stuff as you did in your Activity
    
        return root;
    
    }
    

    As a note, Fragments don't have the findViewById method. You have to call it on the View that you are inflating to be its layout, so in this case you inflated root and then you would call root.findViewById(R.id.some_id) to get a view.