简体   繁体   English

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

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

In list view the position of particular row can easily be determined through array adapter and then by using onlistitemclick method we can have that value in toast message 在列表视图中,可以通过数组适配器轻松确定特定行的位置,然后使用onlistitemclick方法,我们可以在toast消息中获取该值

but how can we get position or id of any particular text view(when it is clicked) from table view where table rows are generating dynamically through xml parser.. where column names are id, name and class. 但是我们如何从表视图中获取任何特定文本视图的位置或id(当它被单击时),其中表行通过xml解析器动态生成..其中列名是id,name和class。 id is auto incremented.. pls suggest. id是自动递增的..请建议。

the code is 代码是

try { 尝试{

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

Invoke ViewGroup method: 调用ViewGroup方法:

public int indexOfChild (View child)

on TableLayout, where child is TableRow. 在TableLayout上,其中child是TableRow。 TableRow should be accesible through TableRow应该可以访问

public final ViewParent getParent ()

of View inside TableRow. 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");
}

Hope that help, but I haven't tested it. 希望有所帮助,但我还没有测试过。

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

相关问题 如何从Android中的数组获取滚动位置到textView? - How to get scroll position to a textView from an array in Android? 如何从表中获取计数并在Android中的TextView上设置为文本 - How to get count from table and set as a text on textview in android Android - 从带有 html 数据的 textview 获取文本位置 - Android - get text position from textview with html data 如何在android TextView中获取点击跨度的x,y位置? - How to get x,y position of clicked span in android TextView? 如何动态创建Imageview或textview在android中点击id或位置? - How To Get Dynamically Created Imageview or textview clicked id or position in android? 如何在Android中的布局中定位TextView? - How to position a TextView in a layout in Android? 如何从 RecyclerView 的适配器替换 Android 中 TextView 的位置? - How to alternate the position of TextView in Android from adapter of RecyclerView? 如何在android中从TextView获取日期到DatePicker? - How to get date from TextView to DatePicker in android? Android如何从TextView获取字符串到字符串 - Android how to get a String from the TextView to a String 如何从水平滚动视图获取textview的第一个和最后一个位置 - how to get first and last position of textview from horizontal scroll view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM