我对 Android 和 Java 编程很陌生,真的需要一些帮助。我试图创建一个从网站加载 .txt 文件的应用程序,将每一行放入一个数组列表中,然后将字符串显示为列表的元素。我添加了一个 sysout 以了解数组中是否有尽可能多的字符串,但它始终为零。我有多种想法如何解决这个问题,但由于静态和非静态字段之间的错误,它们无法正常工作。我不知道如何解决这个问题,所以也许你可以帮助我。
主要活动
public class MainActivity extends Activity {
ListView listview;
String response_str;
String output_str;
ArrayList<String> releases = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ABGXThread thread = new ABGXThread();
thread.start();
listview = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, releases);
listview.setAdapter(arrayAdapter);
System.out.println("Anzahl Releases: "+releases.size());
}
public void getReleases(String release) {
releases.add(release);
}
}
ABGX线程
public class ABGXThread extends Thread{
String response_str;
String output_str;
String[] parts;
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("URL-OF-TXT-HERE");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response_str = client.execute(request, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String cut = "nuked)";
output_str = response_str.substring(response_str.indexOf(cut)+cut.length(), response_str.length());
parts = output_str.split("\n");
System.out.println(parts[6260]);
System.out.println(parts.length);
}
public String getRels(int counter) {
return parts[counter];
}
}