当我在搜索用Jsoup解析的html中的链接时,我正在搜索自己的活动,我偶然发现LinkEnabledTextView 在演示中,它是为字符串实现的,但在我的情况下,我从Web获取文本并在TextView中显示它。
在accepted answer in this question中,它已应用于listitem。我按照两个链接中的说明进行操作但是我有 将问题应用到我的场景中。
NewsDetails类
public class NewsDetails extends AppCompatActivity implements TextLinkClickListener{
TextView newsTitle;
LinkEnabledTextView newsContent;
private String news_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_details);
showDialog();
newsTitle = (TextView) findViewById(R.id.dnews_title);
newsAuthorDate = (TextView) findViewById(R.id.author_date);
newsContent = (LinkEnabledTextView) findViewById(R.id.dnews_content);
check = new LinkEnabledTextView(this, null);
check.setOnTextLinkListener(this);
check.gatherLinksForText(String.valueOf(newsContent));
check.setTextColor(Color.BLUE);
check.setLinkTextColor(Color.RED);
MovementMethod movementMethod = check.getMovementMethod();
if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod)) {
if (check.getLinksClickable()) {
check.setMovementMethod(LinkMovementMethod.getInstance());
}
}
//Skipped unecassry codes
}
private void showDialog() {
internetDialog = new AlertDialog.Builder(NewsDetails.this)
...
}
private void loadNews() {
Log.d(TAG, "loadNews called");
final ProgressBar progressBar;
progressBar = (ProgressBar) findViewById(R.id.progress_circle);
progressBar.setVisibility(View.VISIBLE);
String news_id = getIntent().getStringExtra("NewsId");
Log.d(TAG, "You clicked news id " + news_id);
StringRequest stringRequest = new StringRequest(news_id,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.d("Debug", response.toString());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
parseHtml(response);
newsData = response;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
final AlertDialog.Builder sthWrongAlert = new AlertDialog.Builder(NewsDetails.this);
...
sthWrongAlert.show();
}
});
//Creating requestqueue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request queue
requestQueue.add(stringRequest);
}
private void parseHtml(String response) {
Log.d(TAG, "parsinghtml");
Document document = Jsoup.parse(response);
String news_title = document.select("h1.entry-title").first().text();
news_content = document.select("div.entry-content").first().html();
newsTitle.setText(news_title);
Spanned spanned = Html.fromHtml(news_content, new UILImageGetter(newsContent, this), null );
newsContent.setText(spanned);
//Unhiding views
newsTitle.setVisibility(View.VISIBLE);
newsContent.setVisibility(View.VISIBLE);
}
}
我试图将String.valueOf(newsContent)
传递给check.gatherLinksForText
,但它无效。
我也尝试了check.gatherLinksForText(news_content)
,但它会抛出NPE。
请问我应该通过check.gatherLinksForText()
使其正常工作的正确参数是什么?