[英]Get text of dynamic generated TextView when clicking on it
I'm running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source. 我正在运行for循环并生成应该可单击的TextView,因为我想启动一个intent并将url作为参数和源传递。
So, I've tried this 所以,我试过这个
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//System.out.println(articleURL[v.getId()].getText().toString());
System.out.println(v.getId());
}
});
The problem i encounter is that the v.getId() is always 0. And when i use the commented code 我遇到的问题是v.getId()始终为0.当我使用注释代码时
System.out.println(articleURL[v.getId()].getText().toString());
I get an exception that says 我得到一个例外
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
I just need the content of the TextView i clicked on. 我只需要点击TextView的内容。 How exactly do i get it?
我到底怎么做到了? articleURL[i] doesn't work because he doesn't know i then.
articleURL [i]不起作用,因为他当时不知道。 How can v.getId() always be -1?
v.getId()如何始终为-1? No matter which one I click?
无论我点击哪一个?
This here is the complete for-loop 这是完整的for循环
TextView articleURL = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
articleURL[i] = new TextView(getActivity());
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(articleURL[v.getId()].getText().toString());
//System.out.println(v.getId());
}
});
}
You actually get the View in the parameter. 实际上你在参数中得到了View。 Just cast it to TextView and call
getText()
只需将其强制转换为TextView并调用
getText()
public void onClick(View v) {
Log.d("text",((TextView) v).getText().toString());
}
Also don't use System.out.println
. 也不要使用
System.out.println
。 This is Android, not desktop Java, and coding android is a huge difference to normal Java. 这是Android,而不是桌面Java,编码android与普通Java有很大的不同。 You should get a book on Android and read it, otherwise your apps will start crashing pretty soon and you won't have any chance to fix them.
你应该买一本关于Android的书并阅读它,否则你的应用程序很快就会崩溃,你将没有机会修复它们。
You may try the following: 您可以尝试以下方法:
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(((TextView)v).getText());
}
};
// ... some loop
articleURL[i].setOnClickListener(listener);
If you also want to get index of item, try this: 如果您还想获得项目索引,请尝试以下方法:
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(v.getTag());
}
};
// ... some loop
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(listener);
Try this.. 尝试这个..
use this as globel 用这个作为globel
TextView articleURL[];
and then initial the articleURL like below 然后初始化articleURL,如下所示
articleURL = new TextView[hashMapSize];
and then if your extends fragement means use below 然后,如果你扩展fragement意味着使用下面
articleURL[i] = new TextView(getActivity());
extends activity means 扩展活动意味着
articleURL[i] = new TextView(this);
and 和
System.out.println(((TextView)v).getText().toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.