如何设置一排网格视图中的Andr​​oid?网格、视图、如何设置、oid

2023-09-06 23:38:48 作者:趁早

现在我展示从XML文件中的数据以android.This网格视图是确切的XML的文件链接

http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders这 我试图show.I已经成功地做​​到这一点的概念,但这里的问题是,我没有得到像下面的图片的答案

我需要表现出像下面的图片,在机器人

但我只得到像下面的图片......

如何克服这个概念?任何一个可以请让我清楚了吗?

感谢您的precious时间!..

下面我的消息来源,以供参考,请找

GridviewSample.java

 公共类GridviewSample扩展活动
{

//所有静态变量
静态最终字符串URL =htt​​p://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders;
// XML节点键
静态最后弦乐KEY_TABLE =表; //父节点
静态最后弦乐KEY_CUST =CUST_NAME;
静态最后弦乐KEY_ORDER =Order_No;
静态最后弦乐KEY_FREIGHT =Freight_Rate;
静态最后弦乐KEY_STATION1 =STATION_NAME;
静态最后弦乐KEY_STATION2 =Station_Name1;

@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    GridView控件GV =(GridView控件)findViewById(R.id.gridView1);

    ArrayList的< HashMap的<字符串,字符串>>菜单项=新的ArrayList< HashMap的<字符串,字符串>>();

    XMLParser的分析器=新XMLParser的();

    XML字符串= parser.getXmlFromUrl(URL); //获取XML

    文档DOC = parser.getDomElement(XML); //获取DOM元素

    NodeList的NL = doc.getElementsByTagName(KEY_TABLE);

    //遍历所有的项目节点<项目>

    的for(int i = 0; I< nl.getLength();我++)
    {
        //创建新的HashMap

        HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
        元素e =(元)nl.item(我);

        //添加每个子节点HashMap中的key =>值

        map.put(KEY_CUST,parser.getValue(即KEY_CUST));
        map.put(KEY_ORDER,parser.getValue(即KEY_ORDER));
        map.put(KEY_FREIGHT,parser.getValue(即KEY_FREIGHT));
        map.put(KEY_STATION1,parser.getValue(即KEY_STATION1));
        map.put(KEY_STATION2,parser.getValue(即KEY_STATION2));

        //添加HashList到ArrayList中
        menuItems.add(图)
    }

    //添加菜单项到ListView控件

 SimpleAdapter适配器=新SimpleAdapter(这一点,菜单项,R.layout.grid_item,
 新的String [] {KEY_CUST,KEY_ORDER,KEY_FREIGHT,KEY_STATION1,KEY_STATION2},新的INT []
 {
    R.id.cust,R.id.order,R.id.freight,R.id.statio1,R.id.station2});


    gv.setAdapter(适配器);

    gv.setOnItemClickListener(新OnItemClickListener()
    {

    公共无效onItemClick(适配器视图<>表,视图V,INT位置,长ID)
    {


    字符串卡斯特=((TextView中)v.findViewById(R.id.cust))的getText()的toString()。
    字符串为了=((TextView中)v.findViewById(R.id.order))的getText()的toString()。
    字符串运费=((TextView中)v.findViewById(R.id.freight))的getText()的toString()。
    字符串STATION1 =((TextView中)v.findViewById(R.id.statio1))的getText()的toString()。
    字符串STATION2 =((TextView中)v.findViewById(R.id.station2))的getText()的toString()。

    //开始的新意图

    意图=新的意图(getApplicationContext(),Single_gridview_item.class);

      in.putExtra(KEY_CUST,CUST);
      in.putExtra(KEY_ORDER,顺序);
      in.putExtra(KEY_FREIGHT,运费);
      in.putExtra(KEY_STATION1,STATION1);
      in.putExtra(KEY_STATION2,STATION2);
      startActivity(在);
      }
    });
}}
 

Single_gridview_item

 公共类Single_gridview_item扩展活动
{

// XML节点键

静态最后弦乐KEY_TABLE =表; //父节点
静态最后弦乐KEY_CUST_NAME =CUST_NAME;
静态最后弦乐KEY_ORDER =Order_No;
静态最后弦乐KEY_FREIGHT =Freight_Rate;
静态最后弦乐KEY_STATION1 =STATION_NAME;
静态最后弦乐KEY_STATION2 =Station_Name1;

@覆盖
公共无效的onCreate(包savedInstanceState)
{

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.single_grid_item);

    //获取意图数据
    意图= getIntent();

    //从previous意图XML值
    字符串卡斯特= in.getStringExtra(KEY_CUST_NAME);
    字符串为了= in.getStringExtra(KEY_ORDER);
    字符串运费= in.getStringExtra(KEY_FREIGHT);
    字符串STATION1 = in.getStringExtra(KEY_STATION1);
    字符串STATION2 = in.getStringExtra(KEY_STATION2);

    //显示所有的值在屏幕上

    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(卡斯特);
    lblorder.setText(订单);
    lblfreight.setText(货运);
    lblstation1.setText(STATION1);
    lblstation2.setText(STATION2);
}}
 

XMLParser.java

 公共XMLParser类{

//构造
公共XMLParser的(){

}

/ **
 *从URL获取XML使得HTTP请求
 * @参数的URL字符串
 * * /
公共字符串getXmlFromUrl(字符串URL){
    XML字符串= NULL;

    尝试 {
        // defaultHttpClient
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpPost httpPost =新HttpPost(URL);

        HTT presponse HTT presponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = HTT presponse.getEntity();
        XML = EntityUtils.toString(httpEntity);

    }赶上(UnsupportedEncodingException E){
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    //返回XML
    返回XML;
}

/ **
 *获取XML DOM元素
 * @参数XML字符串
 * * /
公开文件getDomElement(XML字符串){
    文档DOC = NULL;
    DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
    尝试 {

        DocumentBuilder的DB = dbf.newDocumentBuilder();

        InputSource的是=新的InputSource();
            is.setCharacterStream(新StringReader(XML));
            DOC = db.parse(是);

        }赶上(的ParserConfigurationException E){
            Log.e(错误,e.getMessage());
            返回null;
        }赶上(的SAXException E){
            Log.e(错误,e.getMessage());
            返回null;
        }赶上(IOException异常E){
            Log.e(错误,e.getMessage());
            返回null;
        }

        返回文档;
}

/ **获取节点值
  * @参数ELEM元素
  * /
 公共最后弦乐getElementValue(节点ELEM){
     子节点;
     如果(ELEM!= NULL){
         如果(elem.hasChildNodes()){
             对于(子= elem.getFirstChild();!孩子= NULL;孩子= child.getNextSibling()){
                 如果(child.getNodeType()== Node.TEXT_NODE){
                     返回child.getNodeValue();
                 }
             }
         }
     }
     返回 ;
 }

 / **
  *获取节点值
  * @参数元素节点
  * @参数密钥字符串
  * * /
 公共字符串的getValue(元素项,字符串str){
        节点列表N = item.getElementsByTagName(STR);
        返回this.getElementValue(n.item(0));
    }}
 

解决方案

不要使用网格视图,它不会帮你。它的目的是要显示的项目作为一格的列表。可以指定列数,但显示表是更加作业的要TableLayout

Right now i am displaying the data from XML file in to grid view in android.This is the exact xml's file link

"http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders" which i am trying to show.I have done that concept successfully, but here the problem is,i am not getting the answer like the below image

i need to show like the below image, in android

but i am getting only like the below image.....

How to overcome this concept?can any one please make me clear?

thanks for your precious time!..

Here my sources for reference,please find

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


    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 keys

static final String KEY_TABLE = "Table"; // parent node
static 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";

@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_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 {

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

解决方案

Don't use a grid view, it won't help you. It is designed to display a list of items as a grid. You may specify the number of columns, but displaying a table is more of a job for a TableLayout.

 
精彩推荐
图片推荐