Marcos Guimaraes
Marcos Guimaraes

Reputation: 1295

Android Activity methods in Fragment

I have a Fragment activity that I am using to make a tab bar. Inside this activity I am planning to put a table or ListViews with some random values.

Here's my code:

import com.example.mvaguimaraes.beetrackprov.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;


public class Orders extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.orders, container, false);

        ListView listView1 = (ListView) findViewById(R.id.listView1);


        Order[] items = {
                new Order(1, "Milk", 21.50),
                new Order(2, "Butter", 15.99),
                new Order(3, "Yogurt", 14.90),
                new Order(4, "Toothpaste", 7.99),
                new Order(5, "Ice Cream", 10.00),
        };

        ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
                android.R.layout.simple_list_item_1, items);

        listView1.setAdapter(adapter);

        listView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {

                String item = ((TextView) view).getText().toString();

                Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

            }
        });

    }

}

The problem is that I cannot extend my Orders class to Fragment and Activity so I'm getting this errors in my code:

Cannot resolve method findViewByID(int)

ListView listView1 = (ListView) findViewById(R.id.listView1);

Cannot resolve constructor

ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
                android.R.layout.simple_list_item_1, items);

Cannot resolve method getBaseContext()

Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

Probably because they inherit something from Activity. There's any way to make them work inside the Fragment? Sorry for my noob-ness I am new to Android Development.

To open another screen after clicking on one of the items of the listview I did this right after the onItemClick public void method:

String item = ((TextView) view).getText().toString();

                Intent i = new Intent(getApplicationContext(), OrderDetails.class);
                i.putExtra("new_variable_name",item);

                Intent browserIntent =
                        new Intent(Orders.this, OrderDetails.class);

                startActivity(browserIntent);
                startActivity(i);

I tried to replace both "getApplicationContext()" and "Orders.this" with "getActivity()" but it didn't work.

Upvotes: 0

Views: 222

Answers (1)

George Mulligan
George Mulligan

Reputation: 11903

Fragments do not have a findViewById() method like Activities. Instead you call findViewById() to lookup views on the View returned from inflating the layout in onCreateView(). Also, where you need a Context you typically use the Activity the fragment is attached to by calling getActivity().

public class Orders extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.orders, container, false);
        ListView listView1 = (ListView) v.findViewById(R.id.listView1);

        Order[] items = {
                new Order(1, "Milk", 21.50),
                new Order(2, "Butter", 15.99),
                new Order(3, "Yogurt", 14.90),
                new Order(4, "Toothpaste", 7.99),
                new Order(5, "Ice Cream", 10.00),
        };

        ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(getActivity(),
                android.R.layout.simple_list_item_1, items);

        listView1.setAdapter(adapter);

        listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                String item = ((TextView) view).getText().toString();
                Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
            }
        });

        return v;
    }
}

Upvotes: 2

Related Questions