我有一个Continent.txt
文件放置在我的res/raw文件夹中。里面包含以下内容。使用Jsoup解析文本文件
<div class="continents">
<a href="#US">US</a>
<a href="#CA">Canada</a>
<a href="#EU">Europe</a>
</div>
我能够解析美国,加拿大,欧洲使用jsoup文本,但是当我把它们展示给一个TextView,他们在一行中显示。输出看起来像这样。
美国加拿大欧洲
我想要的输出中是这样。
美国
加拿大
欧洲
这是我的代码。
package com.example.readfile;
import java.io.InputStream;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txtContinent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtContinent = (TextView) findViewById(R.id.textView1);
new MyTask().execute();
}
class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {
ArrayList<String> arr_linkText = new ArrayList<String>();
@Override
protected ArrayList<String> doInBackground(Void... params) {
Document doc;
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.continent);
byte[] b = new byte[in_s.available()];
in_s.read(b);
doc = Jsoup.parse(new String(b));
Element link = doc.select("a").first();
String text = doc.body().text();
arr_linkText.add(text);
} catch (Exception e) {
// e.printStackTrace();
txtContinent.setText("Error: can't open file.");
}
return arr_linkText; // << retrun ArrayList from here
}
@Override
protected void onPostExecute(ArrayList<String> result) {
for (String temp_result : result) {
txtContinent.append(temp_result + "\n");
}
}
}
}
我不知道如何逐行阅读文件,我希望有人可以向我说明。谢谢!
你是老板。谢谢你帮助我! – Questions