好吧,这让我疯了,我正在向你们寻求帮助,因为我喜欢自己解决问题,这是一场噩梦。我正在制作一个应用程序,用于学习目的,解析reddit首页的儿子数据,并将信息填充到列表视图中。我有几节课:
下载程序类
public class DataDownloader extends AsyncTask<String, Void, String> {
private String downloadedData;
@Override
protected String doInBackground(String... params) {
downloadedData = downloadData(params[0]);
if(downloadedData == null){
Log.d("DataDownloader", "There was an error downloading the file");
}
return downloadedData;
}
@Override
protected void onPostExecute(String downloadedFile) {
super.onPostExecute(downloadedFile);
}
private String downloadData(String urlPath){
StringBuilder buffer = new StringBuilder();
try{
URL urlAddress = new URL(urlPath);
HttpURLConnection connect = (HttpURLConnection) urlAddress.openConnection();
int serverResponse = connect.getResponseCode();
Log.d("DataDownloader", "Server responded with response code: " + serverResponse);
InputStream stream = connect.getInputStream();
InputStreamReader streamReader = new InputStreamReader(stream);
int charRead;
char[] inputBuffer = new char[500];
while(true){
charRead = streamReader.read(inputBuffer);
if(charRead <= 0){
break;
}
buffer.append(String.copyValueOf(inputBuffer, 0, charRead));
}
return buffer.toString();
} catch (MalformedURLException e) {
Log.d("DataDownloader", "MalformedURLException");
} catch (IOException e) {
Log.d("DataDownloader", "IOException");
}
return null;
}
}
JSON解析器类
public class JSONParser {
private String jsonData;
private ArrayList<Submission> submissions;
public JSONParser(String jsonData) {
this.jsonData = jsonData;
submissions = new ArrayList<Submission>();
}
public ArrayList<Submission> getSubmissions() {
return submissions;
}
public void parseJSON() {
Submission submissionContents = null;
if (jsonData != null) {
Log.d("JSONParser.parseJSON", "Looks like JSON data to me");
try {
JSONObject redditJSONObject = new JSONObject(jsonData);
JSONObject data = redditJSONObject.getJSONObject("data");
JSONArray children = data.getJSONArray("children");
for(int i = 0; i < children.length(); i++){
submissionContents = new Submission();
JSONObject submissionDetails = children.getJSONObject(i);
JSONObject submissionInfo = submissionDetails.getJSONObject("data");
submissionContents.setTitle(submissionInfo.getString("title"));
submissionContents.setAuthor(submissionInfo.getString("author"));
submissionContents.setSubReddit(submissionInfo.getString("subreddit"));
submissionContents.setThumbnailLink(submissionInfo.getString("thumbnail"));
submissionContents.setUrlLink(submissionInfo.getString("url"));
submissionContents.setNumComments(submissionInfo.getInt("num_comments"));
submissionContents.setScore(submissionInfo.getInt("score"));
submissions.add(submissionContents);
}
} catch (JSONException j) {
Log.d("JSON.parseJSON", "Error creating JSON object");
}
} else {
Log.d("JSONParser", "The file was empty!");
}
}
}
我必须通过缩略图网址下载每个链接的位图。
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
String imageUrl = params[0];
Bitmap image = null;
try{
URL url = new URL(imageUrl);
InputStream imageStream = url.openStream();
image = BitmapFactory.decodeStream(imageStream);
} catch (Exception e){
Log.d("ImageDownloader", "Error establishing connection to url: " + imageUrl);
}
return image;
}
}
}
我可以轻松地实现一个常规的ArrayAdapter来填充列表,但这只会用文本填充它,我正在寻找一种用图像填充它的方法。我知道我必须创建一个自定义适配器,但老实说我无法搞清楚。我知道你需要创建一个xml,它代表每一行,我已经做到了。此xml包含图像和2个文本视图。我有我的“提交”数组,其中包含每个提交内容(标题,日期等)作为Submission对象,但我还必须在那里的某处下载图像。任何人都能指出我在正确的方向吗?
答案 0 :(得分:1)
如果您正在从网络网址填充图片,则此问题已在Android上解决。
我强烈建议您从Square搜索Picasso或从Google搜索Glide。两者都有非常简单的语法,可以很好地解决您的问题。他们还将处理缓存,并在加载图像和进一步回收问题之前处理视图滚动屏幕的问题。
你们最终会得到类似的东西:
Picasso.with(context).load(imageUrl).into(imageView);
当你正在学习Android时,尝试用更现代的RecyclerView替换你的ListView,它会为你处理循环模式,让你更酷地启动。