简体   繁体   中英

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

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 is auto incremented.. pls suggest.

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:

public int indexOfChild (View child)

on TableLayout, where child is TableRow. TableRow should be accesible through

public final ViewParent getParent ()

of View inside TableRow.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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