MainActivity未填充listview中的数据

时间:2015-06-26 07:12:30

标签: android listview

我正在尝试从此http://dotnetbyexample.blogspot.com/2015/06/custom-oauth-login-to-facebook-for.html填充我的列表视图。

编译和运行应用程序时没有错误,但只有一个空白的MainActivity。这是我的项目代码。

主要活动:

package com.example.szen95.meddict;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new SearchFragment())
                    .commit();
        }
        getSupportActionBar().setElevation(0f);
     }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

SearchFragment:

package com.example.szen95.meddict;

/**
 * Created by szen95 on 6/15/15.
 */

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

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

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

/**
 * A placeholder fragment containing a simple view.
 */
public class SearchFragment extends Fragment {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://dailymed.nlm.nih.gov/dailymed/services/v2/drugclasses.json";

    // JSON Node names
    private static final String TAG_DATA = "data";
    private static final String TAG_CODE = "code";
    private static final String TAG_CODING_SYSTEM = "codingSystem";
    private static final String TAG_TYPE = "type";
    private static final String TAG_NAME = "name";


    // contacts JSONArray
    JSONArray data = null;
//    ArrayAdapter<String> mConditionsAdapter;

    public SearchFragment() {
    }

    ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();

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

        // Retrieving the currently selected item number
//        int position = getArguments().getInt("position");

        // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.fragment_main, container, false);

        new GetData().execute();

        return v;
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetData extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(getActivity());
            //
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }
        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    data = jsonObj.getJSONArray(TAG_DATA);

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

                        String code = c.getString(TAG_CODE);
                        String codingSystem = c.getString(TAG_CODING_SYSTEM);
                        String type = c.getString(TAG_TYPE);
                        String name = c.getString(TAG_NAME);


                        // tmp hashmap for single contact
                        HashMap<String, String> data = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        data.put(TAG_CODE, code);
                        data.put(TAG_CODING_SYSTEM, codingSystem);
                        data.put(TAG_TYPE, type);
                        data.put(TAG_NAME, name);

                        // adding contact to contact list
                        dataList.add(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            SimpleAdapter adapter = new SimpleAdapter(getActivity(), dataList,
                    R.layout.list_item_search, new String[] { TAG_NAME, TAG_CODE}, new int[] { R.id.name,
                    R.id.code});

            setListAdapter(adapter);
        }

    }

    private void setListAdapter(SimpleAdapter adapter) {
    }

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

        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main, menu);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_refresh) {
//            FetchDataTask defTask = new FetchDataTask();
//            defTask.execute();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

以下两项内容将解决您的问题 -

  1. 通过ListFragment而不是片段扩展片段。

  2. 删除以下方法。

     private void setListAdapter(SimpleAdapter adapter) {
    
     }