如何更改listview项目的位置?

时间:2014-09-09 17:57:40

标签: java android listview android-listview parse-platform

我有一个应用程序,它从云中获取数据并将其显示在列表视图中。我得到的数据来自API的两个来源,另一个来自解析云。首先显示来自API的数据,它取得顶级列表视图项的位置,稍后显示来自解析云的数据并占据较低位置。我希望从解析云中获取的数据占据顶部列表视图项中的位置(当检索到数据时)以及从API中取出的数据向下移动。

private class GetContacts 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();
        list.setAdapter(adapter);



    }

    @Override
    protected Void doInBackground(Void... arg0) {
        getIp = getIpAddress();

        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr = sh
                .makeServiceCall(url + getIp, ServiceHandler.GET);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("ads");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> scoreList, ParseException e) {
                    for (int i = 0; i < scoreList.size(); i++) {

                        try {
                            ParseFile file = (ParseFile) scoreList.get(i)
                                    .get("appImage");
                            HashMap<String, String> contact = new HashMap<String, String>();

                            String desc = scoreList.get(i)
                                    .getString("Desc");
                            String AppTitle = scoreList.get(i).getString(
                                    "AppTitle");
                            String appUrl = scoreList.get(i).getString(
                                    "appUrl");
                            String imageUrl = file.getUrl();
                            String Existing_user = scoreList.get(i)
                                    .getString("ExistingUser");
                            if (Existing_user.equals("true")) {
                                existing = true;
                            } else if (Existing_user.equals("false")) {
                                existing = false;
                            }
                            // adding each child node to HashMap key =>
                            // value
                            if (AppTitle != null) {
                                contact.put(TAG_NAME, AppTitle);
                            } else {
                                contact.put(TAG_NAME,
                                        "Ultimate Voice Recorder");
                            }
                            if (desc != null) {
                                contact.put(TAG_DESC, desc);
                            } else {
                                contact.put(TAG_DESC,
                                        "DESC");
                            }

                            if (imageUrl != null) {
                                contact.put(TAG_PHOTO_URL, imageUrl);
                            } else {
                                contact.put(
                                        TAG_PHOTO_URL,
                                        "URL");
                            }
                            if (appUrl != null) {
                                contact.put(TAG_APP_URL, appUrl);
                            } else {
                                contact.put(
                                        TAG_APP_URL,
                                        "AppUrl");
                            }

                            // adding contact to contact list
                            appsList.add(contact);
                            adapter.notifyDataSetChanged();

                        } catch (Exception ex) {
                            ex.printStackTrace();
                            BugSenseHandler.sendException(ex);

                        }
                    }
                    // Toast.makeText(TodaysPicks.this, scoreList.size(),
                    // Toast.LENGTH_SHORT).show();
                    Log.d("score", "Retrieved " + scoreList.size()
                            + " scores");

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

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_APPS);

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

                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_DESC);

                    // Phone node is JSON Object
                    String photoUrl = c.getString(TAG_PHOTO_URL);
                    String appUrl = c.getString(TAG_APP_URL);
                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_DESC, email);
                    contact.put(TAG_PHOTO_URL, photoUrl);
                    contact.put(TAG_APP_URL, appUrl);

                    // adding contact to contact list
                    appsList.add(contact);
                }

            } catch (JSONException e) {
                e.printStackTrace();
                BugSenseHandler.sendException(e);
            }
        } 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
        try {
            if ((pDialog != null) && pDialog.isShowing()) {
                pDialog.dismiss();
            }
        } catch (final Exception e) {
            BugSenseHandler.sendException(e);
        } finally {
            pDialog = null;
        }
        adapter.notifyDataSetChanged();



    }

}

这是数据的显示方式 http://www.image-share.com/ijpg-2687-211.html

1 个答案:

答案 0 :(得分:0)

您可以创建一个自定义对象的ArrayList,它包含您想要的各种信息。然后在ListAdapter中,返回适当的数据。当您的AsyncTask返回时,更新ArrayList以使您的新元素先出现,然后调用ListAdapter.notifyDataSetChanged。