现在我正在将数据从XML文件显示到androID的网格视图中.这是xml的确切文件链接
“ http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders”其中
我正在尝试显示.我已经成功地完成了这个概念,但是这里的问题是,我没有得到下图所示的答案
我需要在AndroID中显示如下图所示
但是我只得到下面的图像…..
如何克服这个概念?有人可以让我说清楚吗?
感谢您的宝贵时间!..
这里我的参考资料,请找到
GrIDvIEwSample.java
public class GrIDvIEwSample extends Activity {// All static variablesstatic final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";// XML node keysstatic final String KEY_table = "table"; // parent nodestatic 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";@OverrIDepublic 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) { 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); } }); } }
Single_grIDvIEw_item
public class Single_grIDvIEw_item extends Activity{// XML node keysstatic final String KEY_table = "table"; // parent nodestatic final String KEY_CUST_name = "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";@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.single_grID_item); // getting intent data Intent in = getIntent(); // Get XML values from prevIoUs intent String cust = in.getStringExtra(KEY_CUST_name); String order = in.getStringExtra(KEY_ORDER); String freight = in.getStringExtra(KEY_FREIGHT); String station1 = in.getStringExtra(KEY_STATION1); String station2 = in.getStringExtra(KEY_STATION2); // displaying all values on the screen TextVIEw lblcust = (TextVIEw) findVIEwByID(R.ID.cust_label); TextVIEw lblorder = (TextVIEw) findVIEwByID(R.ID.order_label); TextVIEw lblfreight = (TextVIEw) findVIEwByID(R.ID.freight_label); TextVIEw lblstation1 = (TextVIEw) findVIEwByID(R.ID.station1_label); TextVIEw lblstation2 = (TextVIEw) findVIEwByID(R.ID.station2_label); lblcust.setText(cust); lblorder.setText(order); lblfreight.setText(freight); lblstation1.setText(station1); lblstation2.setText(station2);}}
XMLParser.java
public class XMLParser {// constructorpublic XMLParser() {}/** * Getting XML from URL making http request * @param url string * */public String getXmlFromUrl(String url) { String xml = null; try { // defaulthttpClIEnt DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(); httpPost httpPost = new httpPost(url); httpResponse httpResponse = httpClIEnt.execute(httpPost); httpentity httpentity = httpResponse.getEntity(); xml = EntityUtils.toString(httpentity); } catch (UnsupportedEnCodingException e) { e.printstacktrace(); } catch (ClIEntProtocolException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } // return XML return xml;}/** * Getting XML DOM element * @param XML string * */public document getDomElement(String xml){ document doc = null; documentBuilderFactory dbf = documentBuilderFactory.newInstance(); try { documentBuilder db = dbf.newdocumentBuilder(); inputSource is = new inputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage()); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage()); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage()); return null; } return doc;}/** Getting node value * @param elem element */ public final String getElementValue( Node elem ) { Node child; if( elem != null){ if (elem.hasChildNodes()){ for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){ if( child.getNodeType() == Node.TEXT_NODE ){ return child.getNodeValue(); } } } } return ""; } /** * Getting node value * @param Element node * @param key string * */ public String getValue(Element item, String str) { NodeList n = item.getElementsByTagname(str); return this.getElementValue(n.item(0)); }}
解决方法:
不要使用网格视图,它不会帮助您.它旨在将项目列表显示为网格.您可以指定列数,但是显示表对于tableLayout来说是更多的工作.
总结以上是内存溢出为你收集整理的如何在Android中为网格视图设置行?全部内容,希望文章能够帮你解决如何在Android中为网格视图设置行?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)