Arrayadapter and android fragment

1

I have a fragment with the following code, the problem I have is that the lines

ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, data); 

and

ruta = (Spinner) ruta.findViewById(R.id.spinner2);

I generate an error, I probe it in an activity and it works correctly, the problem is that I do not understand how to make the compatibility with a fragment

public class Valorar extends Fragment {

        Connection con;
        Spinner ruta;
        PreparedStatement stmt;
        ResultSet rs;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            String ip = "mssql4.gear.host";
            String db = "ciclomapp1";
            String un = "ciclomapp1";
            String passwords = "Mk36-9DX-580";
            ruta = (Spinner) ruta.findViewById(R.id.spinner2);

            String query = "select nombre from Rutas";
            try {
                con = connectionclass (un, passwords, db, ip);
                stmt = con.prepareStatement(query);
                rs = stmt.executeQuery();
                ArrayList<String> data = new ArrayList<String>();
                while (rs.next()) {
                    String id = rs.getString("nombre");
                    data.add(id);
                }
                String[] array = data.toArray(new String[0]);
                ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
                ruta.setAdapter(NoCoreAdapter);

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }


        @SuppressLint("NewApi")
        public Connection connectionclass(String user, String password, String database, String server)
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            Connection connection = null;
            String ConnectionURL = null;
            try
            {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                ConnectionURL = "jdbc:jtds:sqlserver://" + server +"/"+ database + ";user=" + user+ ";password=" + password + ";";
                connection = DriverManager.getConnection(ConnectionURL);
            }
            catch (SQLException se)
            {
                Log.e("error here 1 : ", se.getMessage());
            }
            catch (ClassNotFoundException e)
            {
                Log.e("error here 2 : ", e.getMessage());
            }
            catch (Exception e)
            {
                Log.e("error here 3 : ", e.getMessage());
            }
            return connection;
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_valorar, container, false);
        }

        public interface OnFragmentInteractionListener {
        }
    }
    
asked by zhet 31.10.2016 в 21:39
source

2 answers

0

You're getting a bit out of what is a fragment on Android Android this maybe it will cost you work because it is in English but you have to understand what a fragment is and how it works in an application.

I'll explain just as broadly.

a fragment is an piece of interface which has its own layout and this needs to be content in an activity, if only the fragment could not be ...

What is happening to you is that you are trying to make a reference to an element of a view that the fragment "does not know" that you have, so that you realize that this element is in your layout you have to inflate the view within the onCreateView of the fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // root es una referencia a tu layout donde estan los elementos que necesitas
        View root = inflater.inflate(R.layout.mi_fragment_layout, container, false);

      // para poder usar/hacer referencia a los elementos tienes que hacerlo mediante la variable root
        Spinner ruta = (Spinner) root.findViewById(R.id.spinner2);

        return root;
    }

To get the context ( Context ) in a fragment you do not use this you must use getActivity()

getActivity() - > in a Fragment the Activity with which it is currently associated returns. take a look at this

therefore

ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);

You should change it by

ArrayAdapter NoCoreAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, data); 
    
answered by 31.10.2016 / 22:43
source
0

You are referring to a Spinner, which does not have a view in which it is:

   @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            ...
            ...
            ruta = (Spinner) ruta.findViewById(R.id.spinner2);

You should regularly have a Layout where to take the reference of your sight, in this case your Spinner:

   @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_valorar); // dentro de main_activity.xml se debe encontrar definido tu Spinner.
            ...
            ...
            ...
            ruta = (Spinner) findViewById(R.id.spinner2);

The above applies to an Activity but in your case I see that you extend your Fragment class:

public class Valorar extends Fragment {

Therefore, the above should be done within the onCreateView() method:

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_valorar, container, false);
            ...
            ...
            ...
            ruta = (Spinner) view.findViewById(R.id.spinner2);

I can also tell you that disabling the policy that does not allow operations in the main thread for an application in production is not good practice.

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
answered by 31.10.2016 в 22:29