[英]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.