-1

我有這個活動,我想變成片段形式,所以我可以通過NavBar調用片段。可能嗎?解決這個問題非常困難。我有一個導航菜單,調用每個選項的片段,所以我真的需要把這段代碼變成片段。Android:如何將我的活動變成片段

public class MainActivity extends ListActivity { 
    private String URL_ITEMS = "http://192.168.1.88/asd/getFixture.php"; 
    private static final String TAG_FIXTURE = "fixture"; 
    private static final String TAG_MATCHID = "matchId"; 
    private static final String TAG_TEAMA = "teamA"; 
    private static final String TAG_TEAMB = "teamB"; 
    JSONArray matchFixture = null; 
    ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // Call Async task to get the match fixture 
     new GetFixture().execute(); 
    } 
    private class GetFixture extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 
     @Override 
     protected Void doInBackground(Void... arg) { 
      ServiceHandler serviceClient = new ServiceHandler(); 
      Log.d("url: ", "> " + URL_ITEMS); 
      String json = serviceClient.makeServiceCall(URL_ITEMS,ServiceHandler.GET); 
      // print the json response in the log 
      Log.d("Get match fixture response: ", "> " + json); 
      if (json != null) { 
       try { 
        Log.d("try", "in the try"); 
        JSONObject jsonObj = new JSONObject(json); 
        Log.d("jsonObject", "new json Object"); 
        // Getting JSON Array node 
        matchFixture = jsonObj.getJSONArray(TAG_FIXTURE); 
        Log.d("json aray", "user point array"); 
        int len = matchFixture.length(); 
        Log.d("len", "get array length"); 
        for (int i = 0; i < matchFixture.length(); i++) { 
         JSONObject c = matchFixture.getJSONObject(i); 
         String matchId = c.getString(TAG_MATCHID); 
         Log.d("matchId", matchId); 
         String teamA = c.getString(TAG_TEAMA); 
         Log.d("teamA", teamA); 
         String teamB = c.getString(TAG_TEAMB); 
         Log.d("teamB", teamB); 
         // hashmap for single match 
         HashMap<String, String> matchFixture = new HashMap<String, String>(); 
         // adding each child node to HashMap key => value 
         matchFixture.put(TAG_MATCHID, matchId); 
         matchFixture.put(TAG_TEAMA, teamA); 
         matchFixture.put(TAG_TEAMB, teamB); 
         matchFixtureList.add(matchFixture); 
        } 
       } 
       catch (JSONException e) { 
        Log.d("catch", "in the catch"); 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("JSON Data", "Didn't receive any data from server!"); 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      ListAdapter adapter = new SimpleAdapter(
        MainActivity.this, matchFixtureList, 
        R.layout.list_item, new String[] { 
        TAG_MATCHID, TAG_TEAMA,TAG_TEAMB 
      } 
        , new int[] { 
        R.id.matchId,R.id.teamA, 
        R.id.teamB 
      } 
      ); 
      setListAdapter(adapter); 
     } 
    } 

回答

0

我改變了它。使用這種方法從任何活動

import android.app.Fragment; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 

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

import java.util.ArrayList; 
import java.util.HashMap; 

public class MainFragment extends Fragment { 
    private String URL_ITEMS = "http://192.168.1.88/asd/getFixture.php"; 
    private static final String TAG_FIXTURE = "fixture"; 
    private static final String TAG_MATCHID = "matchId"; 
    private static final String TAG_TEAMA = "teamA"; 
    private static final String TAG_TEAMB = "teamB"; 
    JSONArray matchFixture = null; 
    ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>(); 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.activity_main, container, false); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     // Call Async task to get the match fixture 
     new GetFixture().execute(); 
    } 


    private class GetFixture extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg) { 
      ServiceHandler serviceClient = new ServiceHandler(); 
      Log.d("url: ", "> " + URL_ITEMS); 
      String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET); 
      // print the json response in the log 
      Log.d("Get match fixture response: ", "> " + json); 
      if (json != null) { 
       try { 
        Log.d("try", "in the try"); 
        JSONObject jsonObj = new JSONObject(json); 
        Log.d("jsonObject", "new json Object"); 
        // Getting JSON Array node 
        matchFixture = jsonObj.getJSONArray(TAG_FIXTURE); 
        Log.d("json aray", "user point array"); 
        int len = matchFixture.length(); 
        Log.d("len", "get array length"); 
        for (int i = 0; i < matchFixture.length(); i++) { 
         JSONObject c = matchFixture.getJSONObject(i); 
         String matchId = c.getString(TAG_MATCHID); 
         Log.d("matchId", matchId); 
         String teamA = c.getString(TAG_TEAMA); 
         Log.d("teamA", teamA); 
         String teamB = c.getString(TAG_TEAMB); 
         Log.d("teamB", teamB); 
         // hashmap for single match 
         HashMap<String, String> matchFixture = new HashMap<String, String>(); 
         // adding each child node to HashMap key => value 
         matchFixture.put(TAG_MATCHID, matchId); 
         matchFixture.put(TAG_TEAMA, teamA); 
         matchFixture.put(TAG_TEAMB, teamB); 
         matchFixtureList.add(matchFixture); 
        } 
       } catch (JSONException e) { 
        Log.d("catch", "in the catch"); 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("JSON Data", "Didn't receive any data from server!"); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      ListAdapter adapter = new SimpleAdapter(
        MainFragment.this.getActivity(), matchFixtureList, 
        R.layout.list_item, new String[]{ 
        TAG_MATCHID, TAG_TEAMA, TAG_TEAMB 
      } 
        , new int[]{ 
        R.id.matchId, R.id.teamA, 
        R.id.teamB 
      } 
      ); 
      setListAdapter(adapter); 
     } 
    } 
} 

顯示片段:

private void showFragment(Fragment fragment) { 
    FragmentManager fm = getFragmentManager(); 
    fm.beginTransaction() 
      .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out) 
      .show(fragment) 
      .addToBackStack(null) 
      .commit(); 
} 
+0

刪除@nullable和setListAdapter(適配器)後沒有更多錯誤;但tbale不顯示來自數據庫的任何數據。它似乎setListAdapter(適配器);不工作在片段? –

+0

請幫忙,如何在這個片段中使用setListAdapter,即時貼在那裏。 –

0

您必須更改代碼才能在片段中使用。它並不難。

public void onCreate(Bundle savedInstanceState) { 
    new GetFixture().execute(); 

    super.onCreate(savedInstanceState); 

} 

然後CreateView的用於片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    data = new ArrayList<PlacesModel>(); 

    View rootView = inflater.inflate(R.layout.defaultfragment, container, false); 

    return rootView; 
} 

然後你異步類將工作與上面相同。雖然獲取任何視圖元素只是使用參考rootView.findViewById()

檢查此tutorial爲更好的理解。

+0

三江源,現在即時通訊卡在setListAdapter(適配器);在行的botom上,似乎setListAdapter不能在片段中工作?那行有錯誤,如果我刪除那行應用程序工作正常,但不顯示數據庫中的任何數據。 –

+0

您必須獲取要在其上設置適配器的列表的引用。試試這個list.setAdapter(new TaskItemAdapter(getActivity(),itemList)); –

+0

我應該把這些代碼放在哪裏? –