有效地阅读大量数据

时间:2014-03-05 03:47:23

标签: java android database

我目前正在制作报纸应用程序,该应用程序应该阅读当前docx的文章(我可以更改格式)。我想知道什么是将它们引用到我的应用程序的最佳方式,以便可以通过按下按钮来调用它们。我知道我可以为每篇文章设置单独的布局和活动,但我有大约200篇文章,所以这不太实用。这是我的自定义列表视图,我想在这里以及每个部分中放置文章。

package com.tjo.gmagnaum;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

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


public class CustomizedListView extends Fragment {  // All static variables
    static final String URL = "http://gmag.hossti.com";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    ListView list;
    LazyAdapter adapter;
    ArrayList<HashMap<String, String>> songsList;

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

        View rootView = inflater.inflate(R.layout.news, container, false);
       songsList = new ArrayList<HashMap<String, String>>();

    list=(ListView)  rootView.findViewById(R.id.list);

        new RetrieveXML().execute(URL);

        // Getting adapter by passing xml data ArrayList


        // Click event for single list row                XMLParser parser = new XMLParser();

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }
        });
        return rootView;
    }

    class RetrieveXML extends AsyncTask<String, Void, String> {

        private Exception exception;
        XMLParser parser = new XMLParser();

        protected String doInBackground(String... urls) {
            try {

                return parser.getXmlFromUrl(urls[0]);
            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(String xml) {
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_ID));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);

            }

            adapter=new LazyAdapter(getActivity(), songsList);

            list.setAdapter(adapter);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果有人真正关心你可以设置一个简单的域名(大量免费的域名),并在你的托管的控制面板中应该有一个FTP文件管理器,你可以在其中添加和组织一个非常简单的网址外观的文件完美的代码。 This is what I used希望它可以帮助你们!