2016-05-17 90 views
0

我想將我的片段代碼轉換爲活動代碼,但是當我嘗試運行活動時,活動打開但數據爲空。在這裏我的代碼將片段代碼轉換爲活動代碼

片段

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

設置你的活動佈局在onCreate方法這樣@Override 公共無效的onCreate(捆綁savedInstanceState){ super.onCreate(savedInstanceState); 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

謝謝真的「有用和工作:) –

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(); 
    } 
}