0
public class AndroidJSONParsingActivity extends ListActivity {

    // url to make request
    private static String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";

    static final int MENU_MANUAL_REFRESH = 0;
    static final int MENU_DISABLE_SCROLL = 1;
    static final int MENU_SET_MODE = 2;

    private LinkedList<String> mListItems;
    //private PullToRefreshView mPullRefreshListView;

    // contacts JSONArray
    JSONArray contacts = null;

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

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

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray(TAG_CONTACTS);

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

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String address = c.getString(TAG_ADDRESS);
                String gender = c.getString(TAG_GENDER);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                String home = phone.getString(TAG_PHONE_HOME);
                String office = phone.getString(TAG_PHONE_OFFICE);

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

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}

这是我的解析代码,现在我可以在我的项目中放入 JsonParser 类。

public class PullToRefreshListActivity extends Activity {

static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;

private PullToRefreshView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private static String url = "http://api.androidhive.info/contacts/";
/** Called when the activity is first created. */



private LinkedList<String> mListItems;

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

    mPullRefreshListView = (PullToRefreshView) findViewById(R.id.pull_refresh_list);

    // Set a listener to be invoked when the list should be refreshed.
    mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPullRefreshListView.setLastUpdatedLabel(DateUtils
                    .formatDateTime(getApplicationContext(),
                            System.currentTimeMillis(),
                            DateUtils.FORMAT_SHOW_TIME
                                    | DateUtils.FORMAT_SHOW_DATE
                                    | DateUtils.FORMAT_ABBREV_ALL));

            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });

    ListView actualListView = mPullRefreshListView.getRefreshableView();

    mListItems = new LinkedList<String>();
    mListItems.addAll(Arrays.asList(mStrings));

    mAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mListItems);

    // You can also just use setListAdapter(mAdapter)
    actualListView.setAdapter(mAdapter);
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

    @Override
    protected String[] doInBackground(Void... params) {
        // Simulates a background job.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        return mStrings;
    }

    @Override
    protected void onPostExecute(String[] result) {
        mListItems
                .addFirst("Added after refresh..New Version Coming soon J-elly Bean");
        mAdapter.notifyDataSetChanged();

        // Call onRefreshComplete when the list has been refreshed.
        mPullRefreshListView.onRefreshComplete();

        super.onPostExecute(result);
    }
}

private String[] mStrings = { "A-stro", "B-ender", "C-upcake", "E-clair",
        "F-royo", "G-ingerbread", "H-oneycomb", "I-ce Cream Sandwich ",
        "Android Versions From iamvijayakumar.blogspot.com" };

}

这是我所做的 PullToRefreshListview 的虚拟示例。现在我想集成,以便我可以在 ListView 中显示数据并使用 Pull To Refresh。你能帮我如何整合吗?我将在哪里放置用于解析的代码?请帮我 。

4

1 回答 1

0

在虚拟示例中,您通过使用以下用于使用 Hashmap 列表设置数据来简单地替换适配器创建。TAG_ID 等被用作列。创建一个名为 row 的 xml,并将 4 个 textview 放入行布局中,并为它们命名,如下所示,我在 int 数组中使用了这些名称。然后将此适配器设置为listview。

mAdapter= new SimpleAdapter(this, mylist, R.layout.row,
new String[] {TAG_ID, TAG_NAME, TAG_EMAIL,TAG_PHONE_MOBILE}, new int[] 
   {R.id.tvtagid, R.id.tvtagname, R.id.tvtagemail,R.id.tvtagphone});

在上面的适配器中,mylist 是您的 HashMap 对象的 Arraylist。

于 2012-11-07T07:11:14.023 回答