-3

我正在尝试将xml文件中的数据显示到android的网格视图中,但是此页面显示一个错误,任何人都可以让我清楚.....

GridviewSample.java

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

        }
    });
}}

谢谢你的时间!..

4

1 回答 1

1

您应该使用 AsyncTask 来执行连接,否则主线程可能会卡住,Android 将退出您的应用程序。

于 2012-09-03T12:38:13.763 回答