我正在尝试通过aSync任务使用json数据(来自Web)填充listview。我之前在脚本中创建了一个名为myCars的列表,但是在异步线程内部填充它时遇到了麻烦。
package com.*****.complexlistview;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private List<Car> myCars = new ArrayList<Car>();
protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20; //caps indicate constants
public static final String TAG = MainActivity.class.getSimpleName();//prints name of class without package name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isNetworkAvailable()) {
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); // new thread
getBlogPostsTask.execute();// don't call do in background directly
populateListView();
}else{
Toast.makeText(this, "Network is unavailable", Toast.LENGTH_LONG).show();
}
}
public boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if(networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {
@Override
protected String doInBackground(Object[] params) {
int responseCode = -1;//need to have this variable outside scope of try/catch block
try {
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){ //could have used just 200 value
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
String responseData = new String(charArray);
JSONObject jsonResponse = new JSONObject(responseData);
String status = jsonResponse.getString("status");
Log.v(TAG, status);
JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
for(int i=0; i < jsonPosts.length(); i++ ){
JSONObject jsonPost = jsonPosts.getJSONObject(i);
String title = jsonPost.getString("title");
Log.v(TAG, "Post " + i + ": " + title);
myCars.add(new Car(title, 1994, R.drawable.kanye8080s, "Lovable"));
/*myCars.add(new Car("Ford", 1940, R.drawable.stadiumarcadium, "Needing work"));
myCars.add(new Car("Toyota", 1994, R.drawable.kanye8080s, "Lovable"));
myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));
myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));*/
}
}else{
Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
}
}
catch (MalformedURLException e){
Log.e(TAG, "Exception caught");
}
catch (IOException e){
Log.e(TAG, "Exception caught");
}
catch (Exception e){//must be in this order, this is the last, general catch
Log.e(TAG, "Exception caught");
}
return "Code: " + responseCode;
}
}
private void populateListView() {
ArrayAdapter<Car> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.carsListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Car>{
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, myCars);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// make sure we have a view to work with
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
//find the car to work with
Car currentCar = myCars.get(position);
//fill the view
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentCar.getIconID());
//Make:
TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
makeText.setText(currentCar.getMake());
//Year
TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
yearText.setText("" + currentCar.getYear());
//Condition
TextView conditionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
conditionText.setText(currentCar.getCondition());
return itemView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
myCars列表应该填充在doInBackground函数中。在主活动函数内部之后,它应该调用populateListView函数,该函数将它们连接在一起。我收到数据时遇到了困难。
非常感谢任何帮助,谢谢,
- 24x7
答案 0 :(得分:2)
使用AsyncTask
在Listview中填充数据,您应该覆盖onPostExecute
的{{1}}方法,以调用populateListView()方法。将其作为:
覆盖AsyncTask
课程中的onPostExecute
:
GetBlogPostsTask