0

データがダウンロードされたら、ListView をリロードすることは可能ですか? 基本的に、アプリを 4.1 用に更新していますが、ListView は XML ファイルをダウンロードして解析します。これをバックグラウンド スレッドで実行する必要があることはわかっていますが、タブを介してアクティビティを開くと、画面が空白になります。これを機能させる方法や、バックグラウンド スレッドで使用する必要があるコードの部分がわかりません。誰か助けてください。ありがとうございました。

    public class ThirdActivity extends ListActivity {

    // All static variables
    static final String URL = "http://selectukradio.com/SelectUKSchedule.xml";     //  http://api.androidhive.info/pizza/?format=xml  http://selectukradio.com/SelectUKSchedule.xml
    // XML node keys
    static final String KEY_ITEM = "day"; // parent node   item
    static final String KEY_ID = "link";     //  id
    static final String KEY_NAME = "dj";     // name
    static final String KEY_COST = "time";    // cost
    static final String KEY_DESC = "tempDay";    // description


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


                    try{
                    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

                    XMLParser parser = new XMLParser();
                    String xml = parser.getXmlFromUrl(URL); // getting XML
                    Document doc = parser.getDomElement(xml); // getting DOM element


                    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
                    // looping through all item nodes <item>
                    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_NAME, parser.getValue(e, KEY_NAME));
                        map.put(KEY_COST, parser.getValue(e, KEY_COST));    // "Rs. " +
                        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

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

                    // Adding menuItems to ListView
                    ListAdapter adapter = new SimpleAdapter(ThirdActivity.this, menuItems,
                            R.layout.list_item,
                            new String[] { KEY_NAME, KEY_COST,KEY_DESC,KEY_ID }, new int[] {
                                    R.id.name, R.id.cost, R.id.day, R.id.link });



                    setListAdapter(adapter);

                    // selecting single ListView item
                    ListView lv = getListView();

                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            // getting values from selected ListItem
                            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                            String link = ((TextView) view.findViewById(R.id.link)).getText().toString();



                            // Starting new intent
                            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                            in.putExtra(KEY_NAME, name);
                            in.putExtra(KEY_COST, cost);
                            in.putExtra(KEY_ID, link);
                            startActivity(in);

                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }

    }   




    @Override public boolean onKeyDown(int keyCode, KeyEvent event) {  


        if (keyCode == KeyEvent.KEYCODE_BACK) {             
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            }     

        return super.onKeyDown(keyCode, event); 
        }
}
4

2 に答える 2

0

最終クラスはこちら

public class ThirdActivity extends ListActivity {

// All static variables
static final String URL = "http://selectukradio.com/SelectUKSchedule.xml";     //  http://api.androidhive.info/pizza/?format=xml  http://selectukradio.com/SelectUKSchedule.xml
// XML node keys
static final String KEY_ITEM = "day"; // parent node   item
static final String KEY_ID = "link";     //  id
static final String KEY_NAME = "dj";     // name
static final String KEY_COST = "time";    // cost
static final String KEY_DESC = "tempDay";    // description

String xml = new String();

// Initially set list of items to empty array
ArrayList<HashMap<String, String>> mMenuItems = new ArrayList<HashMap<String, String>>();
SimpleAdapter mAdapter;
UpdateTask mUpdateTask;

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

    // Adding menuItems to ListView
    mAdapter = new SimpleAdapter(ThirdActivity.this, mMenuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_COST,KEY_DESC,KEY_ID },
            new int[] { R.id.name, R.id.cost, R.id.day, R.id.link });
    setListAdapter(mAdapter);

    // selecting single ListView item
    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String link = ((TextView) view.findViewById(R.id.link)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_ID, link);
            startActivity(in);
        }
    });

    // Start an update
    updateMenuItems();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {             
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        return true; // handled event
    }     
    return super.onKeyDown(keyCode, event); 
}

// Call this method to download latest XML file from network and update listview
private void updateMenuItems() {
    if (mUpdateTask == null) {
        mUpdateTask = new UpdateTask();
        mUpdateTask.execute(URL);
    }
}

// Async task to download XML on background thread
private class UpdateTask extends AsyncTask<String, Void, XMLParser> {

    // This method runs on a background.
    // Do network operation and returns XML parser, or null on exception
    protected XMLParser doInBackground(String... url) {
        try {
            XMLParser parser = new XMLParser();
            xml = parser.getXmlFromUrl(url[0]);
         //   xml = parser.getXmlFromUrl(URL); // getting XML
            return parser;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // This method runs on the main thread when the async task completes.
    // Make sure you only update your UI from this method!
    protected void onPostExecute(XMLParser parser) {
        if (parser == null) {
            // Download failed, display error message or something
            return;
        }

     //   String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        // looping through all item nodes <item>
        mMenuItems.clear();
        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_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, parser.getValue(e, KEY_COST));    // "Rs. " +
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

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

        // all done, notify listview adapter to update
        mAdapter.notifyDataSetChanged();
    }
}
}
于 2013-09-12T21:28:23.587 に答える