如何将此ListActivity代码转换为Fragment或ListFragment。我不会将 xml 代码解析为片段以在自定义列表视图上显示解析的文档。
xml文件的网址:http: //apeiron.comyr.com/fit.xml
申请代码:
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
//static final String URL = "http://api.androidhive.info/pizza/?format=xml";
static final String URL = "http://apeiron.comyr.com/fit.xml";
// XML node keys
static final String KEY_ITEM = "novost"; // parent node
//static final String KEY_ID = "id";
static final String KEY_NAME = "naslov";
static final String KEY_COST = "datum";
static final String KEY_DESC = "text";
static final String KEY_LINK = "link";
static final String KEY_LINK1 = "link1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
final 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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_row,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.naslov, R.id.novost, R.id.datum });
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.naslov)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) view.findViewById(R.id.novost)).getText().toString();
String link=KEY_LINK;
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
in.putExtra(KEY_LINK, link);
in.putExtra("link1", KEY_LINK1);
startActivity(in);
}
});
}
}
我需要做什么 ?
我尝试了这段代码......但应用程序被破坏了:
import android.app.ListFragment;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/** This is a listfragment class */
public class AndroidFragment extends ListFragment {
// All static variables
//static final String URL = "http://api.androidhive.info/pizza/?format=xml";
static final String URL = "http://apeiron.comyr.com/fit.xml";
static final String KEY_ITEM = "novost";
static final String KEY_NAME = "naslov";
static final String KEY_COST = "datum";
static final String KEY_DESC = "text";
static final String KEY_LINK = "link";
static final String KEY_LINK1 = "link1";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
//setListAdapter(adapter);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
final 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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), menuItems,
R.layout.list_row,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.naslov, R.id.novost, R.id.datum });
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.naslov)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) view.findViewById(R.id.novost)).getText().toString();
// Starting new intent
/*Intent in = new Intent(getActivity().getBaseContext(), Asd.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
in.putExtra(KEY_LINK, link);
in.putExtra("link1", KEY_LINK1);
startActivity(in);*/
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
/** Setting the multiselect choice mode for the listview */
// getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
日志猫:
03-12 23:13:44.300: E/AndroidRuntime(3922): FATAL EXCEPTION: main
03-12 23:13:44.300: E/AndroidRuntime(3922): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarnavtab/in.wptrafficanalyzer.actionbarnavtab.MainActivity}: android.os.NetworkOnMainThreadException
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread.access$600(ActivityThread.java:153)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.os.Looper.loop(Looper.java:137)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread.main(ActivityThread.java:5204)
03-12 23:13:44.300: E/AndroidRuntime(3922): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 23:13:44.300: E/AndroidRuntime(3922): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 23:13:44.300: E/AndroidRuntime(3922): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
03-12 23:13:44.300: E/AndroidRuntime(3922): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-12 23:13:44.300: E/AndroidRuntime(3922): at dalvik.system.NativeStart.main(Native Method)
03-12 23:13:44.300: E/AndroidRuntime(3922): Caused by: android.os.NetworkOnMainThreadException
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-12 23:13:44.300: E/AndroidRuntime(3922): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-12 23:13:44.300: E/AndroidRuntime(3922): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-12 23:13:44.300: E/AndroidRuntime(3922): at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-12 23:13:44.300: E/AndroidRuntime(3922): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-12 23:13:44.300: E/AndroidRuntime(3922): at in.wptrafficanalyzer.actionbarnavtab.XMLParser.getXmlFromUrl(XMLParser.java:46)
03-12 23:13:44.300: E/AndroidRuntime(3922): at in.wptrafficanalyzer.actionbarnavtab.AndroidFragment.onCreateView(AndroidFragment.java:50)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.Fragment.performCreateView(Fragment.java:1695)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.BackStackRecord.run(BackStackRecord.java:682)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.Activity.performStart(Activity.java:5117)
03-12 23:13:44.300: E/AndroidRuntime(3922): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272)
03-12 23:13:44.300: E/AndroidRuntime(3922): ... 11 more