2012-09-05 19 views
-1
<NewDataSet> 
<Table> 
<Cust_Name>Dev</Cust_Name> 
<Order_No>OR00000009</Order_No> 
<Freight_Rate>68000</Freight_Rate> 
<Station_Name>Chennai</Station_Name> 
<Station_Name1>Kolkatta</Station_Name1> 
</Table> 
<Table> 
<Cust_Name>FSL</Cust_Name> 
<Order_No>OR00000010</Order_No> 
<Freight_Rate>6000</Freight_Rate> 
<Station_Name>Mumbai</Station_Name> 
<Station_Name1>Pune</Station_Name1> 
</Table> 
<Table> 
<Cust_Name>FSL</Cust_Name> 
<Order_No>OR00000011</Order_No> 
<Freight_Rate>7000</Freight_Rate> 
<Station_Name>Mumbai</Station_Name> 
<Station_Name1>Pune</Station_Name1> 
</Table> 
<Table> 
<Cust_Name>FSL</Cust_Name> 
<Order_No>OR00000012</Order_No> 
<Freight_Rate>7000</Freight_Rate> 
<Station_Name>Chennai</Station_Name> 
<Station_Name1>Bangalore</Station_Name1> 
</Table> 
</NewDataSet> 

如何在android中的gridview行中安排Cust_Name,Order_No,Freight_Rate等.. ..在Android中的網格視圖行中安排xml數據

這是確切的鏈接,上述XML文件

「http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders」

源頭參考

public class GridSample 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"; 

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

    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)); 

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

    // Adding menuItems to ListView 

    ListAdapter adapter = new SimpleAdapter(this, menuItems, 
    R.layout.grid_item,new String[] { KEY_CUST, KEY_ORDER, KEY_FREIGHT }, 
    new int[] { R.id.name, R.id.desciption, R.id.cost }); 

    GridView gv = (GridView)findViewById(R.id.gridVi); 
    gv.setAdapter(adapter); 

    gv.setOnItemClickListener(new OnItemClickListener() 
    { 

    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 description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); 

     // Starting new intent 
     Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
     in.putExtra(KEY_CUST, name); 
     in.putExtra(KEY_ORDER, cost); 
     in.putExtra(KEY_FREIGHT, description); 
     startActivity(in); 
     } 
    }); 
}} 

SingleMenuItemActivity.java

public class SingleMenuItemActivity extends Activity { 

// XML node keys 
static final String KEY_CUST = "Cust_Name"; 
static final String KEY_ORDER = "Order_No"; 
static final String KEY_FREIGHT = "Freight_Rate"; 
@Override 
public 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); 
    String order = in.getStringExtra(KEY_ORDER); 
    String freight = in.getStringExtra(KEY_FREIGHT); 

    // 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); 

    lblcust.setText(cust); 
    lblorder.setText(order); 
    lblfreight.setText(freight); 
}} 

XMLParser.java

public class XMLParser 
{ 

// constructor 
public 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)); 
    }} 

非常感謝!..

+0

你已經嘗試解析XML? – Nikhil

+0

@尼克....我會更新我的問題與我試過.....? – Rohith

回答

-1

您可以設置水平的方式XML文件的佈局和嘗試在GridView控件