我正在尝试使用字符串数组中的值填充ListView。字符串数组中有值,因为我已经检查过以确保。我正在使用的代码,从我之前的搜索中看到的,是正确的。我正在通过Android模拟器运行应用程序,并尝试创建新的模拟器,清理项目,然后重新启动Eclipse。但是当应用程序启动时,列表中没有数据。
我正在使用运行Android 4.2的Galaxy Nexus模拟器。
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView redditView = (ListView) findViewById(R.id.redditView);
String[] redditList = readRedditFeed();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, redditList);
redditView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public String[] readRedditFeed()
{
return output;
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/redditView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
答案 0 :(得分:0)
您正在解析主线程中的数据,它可能会给出ANR。
使用Thread和handler或AsyncTask。
从线程使用处理程序获取解析结果后,在列表中设置适配器 要么 使用 AsyncTask doInbackground()方法接收和解析数据和onPostExecute() 在列表中设置适配器。
答案 1 :(得分:0)
只需使用asyncTask即可完成后台工作而不会阻止UI。
private LoadingStuffTask task;
private String [] redditList;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView redditView = (ListView) findViewById(R.id.redditView);
task = new LoadingStuffTask();
task.execute("Process started!");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class LoadingStuffTask extends
AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
redditList=readRedditFeed()
}
@Override
protected void onProgressUpdate(
String... values) {
}
@Override
protected void onPostExecute(String result) {
pdia.dismiss();
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, redditList);
redditView.setAdapter(adapter);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pdia = new ProgressDialog(classificaA.this);
pdia.setMessage("Doing background..");
pdia.show();
}
}
public String[] readRedditFeed()
{
ArrayList<String> redditList = new ArrayList<String>();
try
{
URL url = new URL("http://www.reddit.com/.json");
InputStream in = url.openStream();
DataInputStream din = new DataInputStream(new BufferedInputStream(in));
String line = "";
String lines = "";
while((line = din.readLine()) != null)
lines += line;
JSONObject json = (JSONObject) new JSONTokener(lines).nextValue();
json = (JSONObject) json.getJSONObject("data");
JSONArray jsonArr = (JSONArray) json.getJSONArray("children");
for(int i = 0; i < 5; i++)
{
json = jsonArr.getJSONObject(i);
json = (JSONObject) json.getJSONObject("data");
redditList.add(json.get("title").toString().replaceAll("\"", ""));
}
}
catch (Exception e)
{}
String[] output = new String[redditList.size()];
redditList.toArray(output);
return output;
}
}