在android中将xml数据显示到gridview中
我正在尝试在android中显示从xml文件到网格视图的数据,但是这个页面显示了一个错误,有人能告诉我清楚吗 GridviewSample.java在android中将xml数据显示到gridview中,android,gridview,xml-parsing,Android,Gridview,Xml Parsing,我正在尝试在android中显示从xml文件到网格视图的数据,但是这个页面显示了一个错误,有人能告诉我清楚吗 GridviewSample.java public class GridviewSample extends Activity { // All static variables static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders"; //
public class GridviewSample extends Activity
{
// All static variables
static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";
// XML node keys
static final String KEY_TABLE = "Table"; // parent node
static final String KEY_CUST = "Cust_Name";
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
static final String KEY_STATION1 = "Station_Name";
static final String KEY_STATION2 = "Station_Name1";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv = (GridView)findViewById(R.id.gridView1);
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_TABLE);
// 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_CUST, parser.getValue(e, KEY_CUST));
map.put(KEY_ORDER, parser.getValue(e, KEY_ORDER));
map.put(KEY_FREIGHT, parser.getValue(e, KEY_FREIGHT));
map.put(KEY_STATION1, parser.getValue(e, KEY_STATION1));
map.put(KEY_STATION2, parser.getValue(e, KEY_STATION2));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
SimpleAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.grid_item,
new String[] { KEY_CUST, KEY_ORDER, KEY_FREIGHT,KEY_STATION1,KEY_STATION2 }, new int[]
{
R.id.cust, R.id.order, R.id.freight,R.id.statio1,R.id.station2 });
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> Table, View v,int position, long id)
{
// getting values from selected GridItem
String cust = ((TextView) v.findViewById(R.id.cust)).getText().toString();
String order = ((TextView) v.findViewById(R.id.order)).getText().toString();
String freight = ((TextView) v.findViewById(R.id.freight)).getText().toString();
String station1 = ((TextView) v.findViewById(R.id.statio1)).getText().toString();
String station2 = ((TextView) v.findViewById(R.id.station2)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), Single_gridview_item.class);
in.putExtra(KEY_CUST, cust);
in.putExtra(KEY_ORDER, order);
in.putExtra(KEY_FREIGHT, freight);
in.putExtra(KEY_STATION1, station1);
in.putExtra(KEY_STATION2, station2);
startActivity(in);
}
});
}}
公共类GridviewSample扩展活动
{
//所有静态变量
静态最终字符串URL=”http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";
//XML节点密钥
静态最终字符串KEY\u TABLE=“TABLE”//父节点
静态最终字符串KEY\u CUST=“CUST\u Name”;
静态最终字符串键\u ORDER=“订单号”;
静态最终字符串键\运费=“运费\费率”;
静态最终字符串键\u STATION1=“Station\u Name”;
静态最终字符串键\u STATION2=“Station\u Name1”;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.gridView1);
ArrayList menuItems=新建ArrayList();
XMLParser=新的XMLParser();
字符串xml=parser.getXmlFromUrl(URL);//获取xml
Document doc=parser.getDomeElement(xml);//获取DOM元素
NodeList nl=doc.getElementsByTagName(键表);
//循环通过所有项目节点
对于(int i=0;ivalue
put(KEY_CUST,parser.getValue(e,KEY_CUST));
put(KEY_-ORDER,parser.getValue(e,KEY_-ORDER));
put(KEY_-FREIGHT,parser.getValue(e,KEY_-FREIGHT));
put(KEY_STATION1,parser.getValue(e,KEY_STATION1));
put(KEY_STATION2,parser.getValue(e,KEY_STATION2));
//将哈希列表添加到ArrayList
menuItems.add(地图);
}
//向ListView添加菜单项
SimpleAdapter=新SimpleAdapter(此,菜单项,R.layout.grid_项,
新字符串[]{KEY_CUST,KEY_ORDER,KEY_运费,KEY_STATION1,KEY_STATION2},新int[]
{
R.id.cust、R.id.order、R.id.freight、R.id.statio1、R.id.station2});
gv.设置适配器(适配器);
gv.setOnItemClickListener(新的OnItemClickListener()
{
public void onItemClick(AdapterView表格、视图v、整型位置、长id)
{
//从选定的GridItem获取值
字符串cust=((TextView)v.findviewbyd(R.id.cust)).getText().toString();
字符串顺序=((TextView)v.findviewbyd(R.id.order)).getText().toString();
字符串运费=((TextView)v.findviewbyd(R.id.freight)).getText().toString();
字符串station1=((TextView)v.findviewbyd(R.id.statio1)).getText().toString();
字符串station2=((TextView)v.findviewbyd(R.id.station2)).getText().toString();
//开始新的意图
Intent in=newintent(getApplicationContext(),Single\u gridview\u item.class);
in.putExtra(关键客户,客户);
in.putExtra(键顺序、顺序);
in.putExtra(关键运费、运费);
in.putExtra(钥匙位置1,位置1);
in.putExtra(钥匙位置2,位置2);
星触觉(in);
}
});
}}
谢谢你抽出时间 您应该使用AsyncTask来执行连接,否则主线程可能会卡住,Android将退出您的应用程序。它显示了什么错误?让我猜猜。。。NetworkOnMainThreadException