繁体   English   中英

Android:如何从表中获取textview位置

[英]Android: how to get textview position from table

在列表视图中,可以通过数组适配器轻松确定特定行的位置,然后使用onlistitemclick方法,我们可以在toast消息中获取该值

但是我们如何从表视图中获取任何特定文本视图的位置或id(当它被单击时),其中表行通过xml解析器动态生成..其中列名是id,name和class。 id是自动递增的..请建议。

代码是

尝试{

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();


        URL sourceUrl = new URL("http://ip-address/test/player.xml");


        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }


    DataList dataList = XMLHandler.DataList;


    name = new TextView[dataList.getName().size()];
    class = new TextView[dataList.getName().size()];

    /** Set the result text in textview and add it to layout */
    for (int i = 0; i < DataList.getName().size(); i++) {

         TableRow tr = new TableRow(this);
         tr.setId(100+i);
         tr.setLayoutParams(new LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT)); 

         // Create a TextView to house the name of the province
         name[i]= new TextView(this);
         name[i].setId(200+i);
         name[i].setText(dataList.getName().get(i));
         name[i].setTextColor(Color.WHITE);
         name[i].setLayoutParams(new LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         tr.addView(name[i]);

         class[i] = new TextView(this);
         class[i].setId(i);
         class[i].setText(dataList.getClass().get(i));
         class[i].setTextColor(Color.WHITE);
         class[i].setLayoutParams(new LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         tr.addView(class[i]);


        t1.addView(tr, new TableLayout.LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
     }
}

调用ViewGroup方法:

public int indexOfChild (View child)

在TableLayout上,其中child是TableRow。 TableRow应该可以访问

public final ViewParent getParent ()

TableRow中的View。

TableRow tableRow = (TableRow)mTextView.getParent();    

int index = -1;

if(parent != null){
    index = mTable.indexOfChild(tableRow);
}

if(index < 0){
    throw new IllegalStateException("TextView not found");
}

希望有所帮助,但我还没有测试过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM