当我从远程文件文本中读取值时,如何在微调器(DropDownList)Android上设置它?
这是教程: Android read text file from internet
感谢您的帮助。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.os.Bundle;
import android.widget.Spinner;
import android.app.Activity;
public class SpinnerPopText extends Activity {
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerpoptext);
new Thread() {
@Override
public void run() {
String path = "http://www.myhost.com/public/country.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u
.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer);
bo.write(buffer);
runOnUiThread(new Runnable() {
@Override
public void run() {
sp = (Spinner) findViewById(R.id.my_spinner);
//And here ????
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
答案 0 :(得分:0)
取一个数组并将值存储在其中......并将其设置在微调器中......
List<String> collection = new ArrayList<String>();
String[] temp;
while ((line = br.readLine()) != null) {
temp = line.split(",");
if (temp.length > 0) {
for (String s: temp) {
collection.add(s);
}
}
}