2016-05-17 35 views
0

フラグメントコードをアクティビティコードに変換しようとしていますが、アクティビティを実行しようとすると、アクティビティは開きますがデータはnullになります。ここに私のコードフラグメントコードをアクティビティコードに変換する

断片

import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.NetworkImageView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.List; 

public class ArmBicepFragment extends Fragment implements AdapterView.OnItemClickListener { 
    // Log tag 
    private static final String TAG = ArmBicepFragment.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "http:......."; 
    private ProgressDialog pDialog; 
    private List<Exercise> exerciseList = new ArrayList<Exercise>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

    public ArmBicepFragment() { 
     // Required empty public constructor 
    } 

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

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

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

     final ListView listView = (ListView) rootView.findViewById(R.id.list); 
     adapter = new CustomListAdapter(getActivity(), exerciseList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(getActivity()); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     if(exerciseList.isEmpty()) { 
      // Creating volley request obj 
      JsonArrayRequest exerciseReq = new JsonArrayRequest(url, 
        new Response.Listener<JSONArray>() { 
         @Override 
         public void onResponse(JSONArray response) { 
          Log.d(TAG, response.toString()); 
          hidePDialog(); 

          exerciseList.clear(); 

          // Parsing json 
          for (int i = 0; i < response.length(); i++) { 
           try { 

            JSONObject obj = response.getJSONObject(i); 
            Exercise exercise = new Exercise(); 
            if (obj.getString("name").equals("bicep")) { 
             exercise.setNama(obj.getString("nama")); 
             exercise.setGambar1(obj.getString("gambar1")); 

             // Genre is json array 

             exerciseList.add(exercise); 
            } 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 

          } 

          // notifying list adapter about data changes 
          // so that it renders the list view with updated data 
          adapter.notifyDataSetChanged(); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
      }); 

      // Adding request to request queue 
      AppController.getInstance().addToRequestQueue(exerciseReq); 
     }else{ 
      hidePDialog(); 
     } 

     listView.setOnItemClickListener(this); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(getContext()); 

     // Include dialog.xml file 
     dialog.setContentView(R.layout.dialog); // layout of your dialog 

     ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbnail1 = (NetworkImageView) dialog.findViewById(R.id.gambar1); 
     thumbnail1.setImageUrl(exerciseList.get(position).getGambar1(), imageLoader); 


     // Set dialog title 
     dialog.setTitle("Detail"); 

     // set values for custom dialog components - text, image and button 
     TextView nama = (TextView) dialog.findViewById(R.id.nama); 
     nama.setText("Nama = " + exerciseList.get(position).getNama()); 
     // similar add statements for other details 

     dialog.show(); 
    } 
} 

そして、ここでフラグメント(何もエラーや警告)

import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.NetworkImageView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Felix on 5/15/2016. 
*/ 
public class RekomendasiExercise extends AppCompatActivity implements AdapterView.OnItemClickListener{ 

    // Log tag 
    private static final String TAG = AbdominalFragment.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "http:............"; 
    private ProgressDialog pDialog; 
    private List<Exercise> exerciseList = new ArrayList<Exercise>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

    public RekomendasiExercise() { 
     // Required empty public constructor 
    } 

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

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 


    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_list, container, false); 
     // Inflate the layout for this fragment 

     final ListView listView = (ListView) rootView.findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, exerciseList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     if(exerciseList.isEmpty()) { 
      // Creating volley request obj 
      JsonArrayRequest exerciseReq = new JsonArrayRequest(url, 
        new Response.Listener<JSONArray>() { 
         @Override 
         public void onResponse(JSONArray response) { 
          Log.d(TAG, response.toString()); 
          hidePDialog(); 

          exerciseList.clear(); 

          // Parsing json 
          for (int i = 0; i < response.length(); i++) { 
           try { 

            JSONObject obj = response.getJSONObject(i); 
            Exercise exercise = new Exercise(); 
            if (obj.getString("nama").equals("abdominal")) { 
             exercise.setNama(obj.getString("nama")); 
             exercise.setGambar1(obj.getString("gambar1")); 

             // Genre is json array 
             // adding movie to movies array 
             exerciseList.add(exercise); 
            } 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 

          } 

          // notifying list adapter about data changes 
          // so that it renders the list view with updated data 
          adapter.notifyDataSetChanged(); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
      }); 

      // Adding request to request queue 
      AppController.getInstance().addToRequestQueue(exerciseReq); 
     }else{ 
      hidePDialog(); 
     } 

     listView.setOnItemClickListener(this); 
     return rootView; 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(this); 

     // Include dialog.xml file 
     dialog.setContentView(R.layout.dialog); // layout of your dialog 

     ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbnail1 = (NetworkImageView) dialog.findViewById(R.id.gambar1); 
     thumbnail1.setImageUrl(exerciseList.get(position).getGambar1(), imageLoader); 


     // Set dialog title 
     dialog.setTitle("Detail"); 

     // set values for custom dialog components - text, image and button 
     TextView nama = (TextView) dialog.findViewById(R.id.nama); 
     nama.setText("Nama = " + exerciseList.get(position).getNama()); 
     // similar add statements for other details 

     dialog.show(); 
    } 
} 
+0

この@Override 公共ボイドのonCreate(バンドルsavedInstanceState){ super.onCreate(savedInstanceState)などOnCreateの方法であなたの活動のレイアウトを設定します。 setContentView(R.layout.activity_list); } –

答えて

3
onCreateView 

から私の変換結果は、アクティビティのライフサイクルの一部ではありません。あなたはonCreateに、このメソッドからのものを移動する必要があります。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list); 

    final ListView listView = (ListView) findViewById(R.id.list); 
    adapter = new CustomListAdapter(this, exerciseList); 
    listView.setAdapter(adapter); 

    pDialog = new ProgressDialog(this); 
    // Showing progress dialog before making http request 
    pDialog.setMessage("Loading..."); 
    pDialog.show(); 

    if(exerciseList.isEmpty()) { 
     // Creating volley request obj 
     JsonArrayRequest exerciseReq = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 

         exerciseList.clear(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           Exercise exercise = new Exercise(); 
           if (obj.getString("nama").equals("abdominal")) { 
            exercise.setNama(obj.getString("nama")); 
            exercise.setGambar1(obj.getString("gambar1")); 

            // Genre is json array 
            // adding movie to movies array 
            exerciseList.add(exercise); 
           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(exerciseReq); 
    }else{ 
     hidePDialog(); 
    } 

    listView.setOnItemClickListener(this); 
} 
+0

本当にありがとう "Helpfullと作品:) –

2

あなたはonCreateであなたのコードを配置する必要があります。下記のコードをコピー/ペーストしてください。

public class RekomendasiExercise extends AppCompatActivity implements AdapterView.OnItemClickListener{ 

    // Log tag 
    private static final String TAG = AbdominalFragment.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "http:............"; 
    private ProgressDialog pDialog; 
    private List<Exercise> exerciseList = new ArrayList<Exercise>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

    public RekomendasiExercise() { 
     // Required empty public constructor 
    } 

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

     setContentView(R.layout.activity_list); 

     final ListView listView = (ListView)findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, exerciseList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     if(exerciseList.isEmpty()) { 
      // Creating volley request obj 
      JsonArrayRequest exerciseReq = new JsonArrayRequest(url, 
        new Response.Listener<JSONArray>() { 
         @Override 
         public void onResponse(JSONArray response) { 
          Log.d(TAG, response.toString()); 
          hidePDialog(); 

          exerciseList.clear(); 

          // Parsing json 
          for (int i = 0; i < response.length(); i++) { 
           try { 

            JSONObject obj = response.getJSONObject(i); 
            Exercise exercise = new Exercise(); 
            if (obj.getString("nama").equals("abdominal")) { 
             exercise.setNama(obj.getString("nama")); 
             exercise.setGambar1(obj.getString("gambar1")); 

             // Genre is json array 
             // adding movie to movies array 
             exerciseList.add(exercise); 
            } 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 

          } 

          // notifying list adapter about data changes 
          // so that it renders the list view with updated data 
          adapter.notifyDataSetChanged(); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
      }); 

      // Adding request to request queue 
      AppController.getInstance().addToRequestQueue(exerciseReq); 
     }else{ 
      hidePDialog(); 
     } 

     listView.setOnItemClickListener(this); 


    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 


    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(this); 

     // Include dialog.xml file 
     dialog.setContentView(R.layout.dialog); // layout of your dialog 

     ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbnail1 = (NetworkImageView) dialog.findViewById(R.id.gambar1); 
     thumbnail1.setImageUrl(exerciseList.get(position).getGambar1(), imageLoader); 


     // Set dialog title 
     dialog.setTitle("Detail"); 

     // set values for custom dialog components - text, image and button 
     TextView nama = (TextView) dialog.findViewById(R.id.nama); 
     nama.setText("Nama = " + exerciseList.get(position).getNama()); 
     // similar add statements for other details 

     dialog.show(); 
    } 
} 
+0

おかげでbtwは' rootView'を取り除いた:D –

0
import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.NetworkImageView; 
import com.elite.youvaa.R; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.List; 

public class ArmBicepFragment extends Activity implements AdapterView.OnItemClickListener { 
    // Log tag 
    private static final String TAG = ArmBicepFragment.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "http:......."; 
    private ProgressDialog pDialog; 
    private List<Exercise> exerciseList = new ArrayList<Exercise>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

    public ArmBicepFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 
     final ListView listView = (ListView)findViewById(R.id.list); 
     adapter = new CustomListAdapter(getApplicationContext(), exerciseList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(getApplicationContext()); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     if(exerciseList.isEmpty()) { 
      // Creating volley request obj 
      JsonArrayRequest exerciseReq = new JsonArrayRequest(url, 
        new Response.Listener<JSONArray>() { 
         @Override 
         public void onResponse(JSONArray response) { 
          Log.d(TAG, response.toString()); 
          hidePDialog(); 

          exerciseList.clear(); 

          // Parsing json 
          for (int i = 0; i < response.length(); i++) { 
           try { 

            JSONObject obj = response.getJSONObject(i); 
            Exercise exercise = new Exercise(); 
            if (obj.getString("name").equals("bicep")) { 
             exercise.setNama(obj.getString("nama")); 
             exercise.setGambar1(obj.getString("gambar1")); 

             // Genre is json array 

             exerciseList.add(exercise); 
            } 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 

          } 

          // notifying list adapter about data changes 
          // so that it renders the list view with updated data 
          adapter.notifyDataSetChanged(); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
      }); 

      // Adding request to request queue 
      AppController.getInstance().addToRequestQueue(exerciseReq); 
     }else{ 
      hidePDialog(); 
     } 

     listView.setOnItemClickListener(this); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 





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


     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(getContext()); 

     // Include dialog.xml file 
     dialog.setContentView(R.layout.dialog); // layout of your dialog 

     ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbnail1 = (NetworkImageView) dialog.findViewById(R.id.gambar1); 
     thumbnail1.setImageUrl(exerciseList.get(position).getGambar1(), imageLoader); 


     // Set dialog title 
     dialog.setTitle("Detail"); 

     // set values for custom dialog components - text, image and button 
     TextView nama = (TextView) dialog.findViewById(R.id.nama); 
     nama.setText("Nama = " + exerciseList.get(position).getNama()); 
     // similar add statements for other details 

     dialog.show(); 
    } 
} 
関連する問題