ListFragment未出现在模拟器上

时间:2015-10-29 23:17:34

标签: android android-viewpager android-listfragment

我的ListFragment有问题,它不会出现在模拟器上,尽管数据是从数据库中提取出来并显示在LogCat上,如下所示:

LoCat

ListFragment

这是我的代码:

ListFragment代码:

package info.androidhive.tabsswipe;

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

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class TopRatedFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View myFragmentView = inflater.inflate(R.layout.fragment_top_rated,
                container, false);

        return myFragmentView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
    ArrayList<HashMap<String, String>> interventionsList;
    // Progress Dialog
    private ProgressDialog pDialog;
 // url to get all interventions list
    private static String url_all_interventions = "http://10.0.2.2/Scripts/liste_interventions.php";
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

     // interventions JSONArray
    JSONArray interventions = null;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        this.getListView().setTextFilterEnabled(true);

        // Hashmap for ListView
        interventionsList = new ArrayList<HashMap<String, String>>();

        // Loading interventions in Background Thread
        new LoadAllInterventions().execute();
    }
    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllInterventions extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading interventions. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONArray json = jParser.makeHttpRequest(url_all_interventions, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Interventions: ", json.toString());

            try {

                    // Getting Array of interventions
                    JSONArray interventions = json;

                    // looping through All interventions
                    for (int i = 0; i < interventions.length(); i++) {
                        JSONObject c = interventions.getJSONObject(i);

                        // Storing each json item in variable
                        String  id = c.getString("id");
                        String name = c.getString("name");

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put("Int_id",   id);
                        map.put("Int_name", name);

                        // adding HashList to ArrayList
                        interventionsList.add(map);
                    }

            } catch (JSONException e) {e.printStackTrace();}
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            getActivity(), interventionsList,
                            R.layout.list_item, new String[] { "Int_id",
                                    "Int_name"},
                            new int[] { R.id.pid, R.id.name });
                    // updating listview
                    setListAdapter(adapter);
                    Log.e("LIST", "setListAdapter is done");
                }
            });

        }

    }
    public void runOnUiThread(Runnable runnable) {
        // TODO Auto-generated method stub

    }

}

这就是ListFragment类的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#99E7E5" >
<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

这是List_item布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/pid"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textStyle="italic" 
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#C0DCC0"
         />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#5B92E5"
android:textSize="17dip"
android:textStyle="bold" />

</LinearLayout>

MainActivity的代码:

package info.androidhive.tabsswipe;

import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "My interventions", "MAP", "My report" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

它的布局:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

请问,在模拟器片段上启用列表外观的问题在哪里?

0 个答案:

没有答案