您好我从此JSON网址加载缩略图图片时出现问题:
http://www.ilovelimerick.ie/?json=1&count=10
我似乎无法查明问题,因为它似乎适用于帖子,日期和时间;标题。那么为什么不用缩略图?
以下是片段中的代码:
public static final int NUMBER_OF_POSTS = 10;
public static final String TAG = NewsFragment.class.getSimpleName();
protected JSONObject mBlogData;
protected ProgressBar mProgressBar;
private final String KEY_TITLE = "title";
private final String KEY_DATE = "date";
private final String KEY_THUMBNAIL = "thumbnail";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news_list,
container, false);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
if(NetworkIsAvailable()) {
mProgressBar.setVisibility(View.VISIBLE);
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
else {
Toast.makeText(getActivity(), "Network is unavailable!", Toast.LENGTH_LONG).show();
}
//String message = getString(R.string.no_items);
//Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
JSONObject jsonPost = jsonPosts.getJSONObject(position);
String blogUrl = jsonPost.getString("url");
Intent intent = new Intent(getActivity(),BlogWebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
}
catch (JSONException e) {
logException(e);
}
}
private void logException(Exception e) {
Log.e(TAG, "Exception caught!", e);
}
private boolean NetworkIsAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if(networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
public void handleBlogResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if (mBlogData == null) {
updateDisplayForError();
}
else {
try {
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
String title = post.getString(KEY_TITLE);
title = Html.fromHtml(title).toString();
String date = post.getString(KEY_DATE);
date = Html.fromHtml(date).toString();
String thumbnail = post.getString(KEY_THUMBNAIL);
thumbnail = Html.fromHtml(thumbnail).toString();
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_TITLE, title);
blogPost.put(KEY_DATE, date);
blogPost.put(KEY_THUMBNAIL, thumbnail);
blogPosts.add(blogPost);
}
String[] keys = { KEY_TITLE, KEY_DATE, KEY_THUMBNAIL };
int[] ids = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), blogPosts, android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
logException(e);
}
}
}
private void updateDisplayForError() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.error_title));
builder.setMessage(getString(R.string.error_message));
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
TextView emptyTextView = (TextView) getListView().getEmptyView();
emptyTextView.setText(getString(R.string.no_items));
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... params) {
int responseCode = -1;
JSONObject jsonResponse = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.ilovelimerick.ie/?json=1&count=10");
try {
HttpResponse response = client.execute(httpget);
StatusLine statusLine = response.getStatusLine();
responseCode = statusLine.getStatusCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null){
builder.append(line);
}
jsonResponse = new JSONObject(builder.toString());
}
else {
Log.i(TAG, String.format("Unsuccessful HTTP response code: %d", responseCode));
}
}
catch (JSONException e) {
logException(e);
}
catch (Exception e) {
logException(e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject result) {
mBlogData = result;
handleBlogResponse();
}
}
以及.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ListView
android:id="@android:id/list"
android:layout_width="235dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="@android:id/empty" >
</ListView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@android:id/empty"
android:layout_toLeftOf="@android:id/list"/>
</RelativeLayout>
答案 0 :(得分:0)
我正在研究类似的问题,但我认为修复很简单。看看你的代码:
1) String[] keys = { KEY_TITLE, KEY_DATE, KEY_THUMBNAIL };
2) int[] ids = { android.R.id.text1, android.R.id.text2 };
3) SimpleAdapter adapter = new SimpleAdapter(getActivity(), blogPosts,
4) android.R.layout.simple_list_item_2, keys, ids);
5) setListAdapter(adapter);
你在做什么是错的,你知道,如果没有,让我解释一下我已添加的行号:
a)在第1行,你创建了一个键值的字符串数组,你创建了一个大小为3的数组,它包含3个字符串对象[这是你的第一个错误,我将解释原因]
b)第2行,你正在创建一个int数组来保存你的id值。这只能容纳2个不同于你的键阵列的物体[第二个错误&amp;我将解释a)&amp; amp; b)用于]
c)现在,在第3和第3行4,您正在创建一个SimpleAdapter,setListAdapter使用它来设置列表中每个项目的布局。让我更清楚地解释一下:http://developer.android.com/reference/android/widget/SimpleAdapter.html - &gt;官方文档
SimpleAdapter设置列表中每个项目的布局。 在您创建的SimpleAdapter中,您指定了一个键数组和一个要使用的ID数组。
简单适配器所做的是循环遍历每个blogPosts,这是一个HashMap键/值对,使用你的键数组,它应该包含&#34; title&#34; &安培; &#34;作者&#34;使用你的ids数组,相应地拥有两个android系统对象,TextView1&amp; TextView2设置android.R.layout.simple_list_item_2布局。
正如您所看到的,您选择的SimpleAdapter只有2行文本,我相信是TextViews。使用第二个参数(blogPosts)并获取与keys数组中第一个键对应的值,将simple_list_item_2项目布局的第一行文本设置为,在您的情况下,在blogPosts中键入KEY_TITLE的值,并对你的ids数组中的下一个id做同样的事情来设置布局&#39; s秒,这是它的最后一行文本。结果如下:
------------------------------- SimpleAdpater的单个列表项---------
第1行:KEY_TITLE:&#34;遇见最伟大的创业公司&#34;
第2行:KEY_AUTHOR:&#34; Jeremy作者&#34;
--------------------------------------结束-------- ----------------------------------
SimpleAdapter为blogPosts ArrayList中的每个blogPost HashMap做同样的事情,当它完成时,setListAdapter使用它来填充你的列表。
您希望SimpleAdapter执行的操作如下:
------------------------------- SimpleAdpater的单个列表项---------
第1行:KEY_TITLE:&#34;遇见最伟大的创业公司&#34;
第2行:KEY_AUTHOR:&#34; Jeremy作者&#34;
第3行:KEY_THUMBNAIL:&#34; {Picture Goes Here}&#34;
---------------------------------------结束------- ----------------------------------
它无法做到这一点,因为您已将android.R.layout.simple_list_item_2作为布局参数传递。
以下是包含评论的正确代码:
/* these next few lines go hand in hand
* ..we construct a String array called keys that holds our two keys, "title" and "author"
* ..then an int array that holds corresponding android system TextView ids
* that will be used by the simple adadpter to display our content in the list view
*
* ..then we create our SimpleAdapter called adapter which will organize our blog posts in
* android's simple_list_item_2 text view layout that has two lines.
* for the paramenters of the SimpleAdapter, we pass in the context, the ArrayList of our blog posts
* and the layout we want, the keys for accessing individual blog post's title and author values
* from the blog posts ArrayList passed in param 2
*
* ..finally, we use setListAdapter to populate our ListView with the blog posts' title and author values
* */
String[] keys = { KEY_TITLE, KEY_AUTHOR };
int[] ids = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, blogPosts,
android.R.layout.simple_list_item_2,
keys, ids);
setListAdapter(adapter);
我现在解决此问题的方法(解决方法)
首先,在ListView上方添加一个ImageView(或者一个图库来保存一堆照片)以保存最新博客文章的图片。
然后,将最新的博客文章作为JSONObject获取,获取该对象的JSONArray,获取它的thumnail值,并使用该缩略图值更新ImageView的顶级图片。
这是针对您的问题的解决方法,因为我目前不知道如何在ListView项目旁边添加图像。