こんにちは、JSON テキストとサムネイル画像を読み込むリストビューがあります。http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/のチュートリアルのコードを使用し、これを xml からロード json に変換しました。
テキストと画像の解析はリストビューで機能しますが、サムネイルと同じ画像を表示したい詳細ビューのような 2 番目のアクティビティを追加しました。lazylist チュートリアルの ImageLoader クラスを使用します。
問題は、詳細ビューでこの画像を読み込めないことです。誰かがこれを機能させる方法を提案していますか?
私のコード CustomizedListView:
`package com.example.androidhive;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.example.androidhive.JSONParser;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListView extends Activity {
// url om request te maken
private static String URL = "http://www.iappministrator.com/OBDE/webservice/android_deelnemers_items.php";
// JSON Node namen
static final String TAG_ITEMS = "items";
static final String TAG_TITLE = "title";
static final String TAG_MESSAGE = "message";
static final String TAG_DATE = "date";
static final String TAG_IMAGES = "images";
// Nieuws JSONArray
JSONArray newsArray = null;
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap voor listView
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
// Maak een JSON Parser instance
JSONParser jParser = new JSONParser();
// Pakt JSON string uit URL
JSONObject json = jParser.getJSONFromUrl(URL);
try{
// Pakt de Array van Nieuwsartikelen
newsArray = json.getJSONArray(TAG_ITEMS);
// Loop door alle Nieuwsartikels
for(int i=0; i < newsArray.length(); i++){
JSONObject c = newsArray.getJSONObject(i);
// Het plaatsen van elk json item in variabele
String title = c.getString(TAG_TITLE);
String message = c.getString(TAG_MESSAGE);
String date = c.getString(TAG_DATE);
String images = c.getString(TAG_IMAGES);
// maak een nieuwe HashMap
HashMap<String, String> map = new HashMap<String, String>();
// voeg elk item child node in de Hashmap -> value
map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, message);
map.put(TAG_DATE, date);
map.put(TAG_IMAGES, images);
// voeg de HashList toe aan ArrayList
newsList.add(map);
}
} catch(JSONException e){
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, newsList, R.layout.list_row,
new String[]{TAG_TITLE, TAG_MESSAGE, TAG_DATE, TAG_IMAGES}, new int[] {
R.id.title, R.id.artist, R.id.duration, R.id.list_image});
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String date = ((TextView) view.findViewById(R.id.duration)).getText().toString();
String message = ((TextView) view.findViewById(R.id.artist)).getText().toString();
//String images = ((TextView) view.findViewById(R.id.list_image)).getText().toString();
//ImageView thumb_image = (ImageView) view.findViewById(R.id.thumbnail); // thumb image
String images = ((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();
//String images = ((ImageView)view.findViewById(R.id.list_image)).toString();
//String images = ((ImageView)view.findViewById(R.id.list_image)).getContext().toString();
// Start de nieuwe intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//Bitmap bitmap = (Bitmap) in.getParcelableExtra(TAG_IMAGES);
in.putExtra(TAG_TITLE, title);
in.putExtra(TAG_DATE, date);
in.putExtra(TAG_MESSAGE, message);
in.putExtra(TAG_IMAGES, images);
startActivity(in);
}
});
}
}`
secondActivity のコード:
package com.example.androidhive;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import com.nostra13.example.universalimageloader.ImageGridActivity.ImageAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class SingleMenuItemActivity extends Activity {
// JSON node keys
private static final String TAG_TITLE = "title";
private static final String TAG_MESSAGE = "message";
private static final String TAG_DATE = "date";
private static final String TAG_IMAGES = "images";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String title = in.getStringExtra(TAG_TITLE);
String date = in.getStringExtra(TAG_DATE);
String message = in.getStringExtra(TAG_MESSAGE);
String images = in.getStringExtra(TAG_IMAGES);
//Bitmap bitmap = in.getParcelableExtra(TAG_IMAGES);
//ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
// Displaying all values on the screen
TextView lblTitle = (TextView) findViewById(R.id.title_label);
TextView lblDate = (TextView) findViewById(R.id.date_label);
TextView lblMessage = (TextView) findViewById(R.id.message_label);
ImageView lblImages = (ImageView) findViewById(R.id.images_label);
//TextView lblImages = (TextView) findViewbyId(R.id.images_label);
// loader image
//int loader = R.drawable.loader;
System.out.println("Ja en nu werkt het niet meer");
// image url
//String image_url = "http://d24w6bsrhbeh9d.cloudfront.net/photo/5614379_460s.jpg";
//ImageLoader imgLoader = new ImageLoader(getApplicationContext());
//-imgLoader.DisplayImage(song.get(CustomizedListView.TAG_IMAGES), lblImages);
System.out.println("Error? haha bam jammer dan:");
//imgLoader.DisplayImage(images, lblImages);
System.out.println("Plaatjes?:"+ images);
lblTitle.setText(title);
lblDate.setText(date);
lblMessage.setText(message);
//lblImages.setImageURI(Uri.parse(images));
//ImageLoader imageLoader = new ImageLoader(getApplicationContext());
//imageLoader.DisplayImage(images,lblImages);
//lblImages.setImageResource(images);
//imageLoader.displayImage(images);
//lblImages.setImageBitmap(bitmap);
//lblImages.setImageResource(R.drawable.bitmap);
//lblImages.setImageResource(images);
/*if (d instanceof BitmapDrawable) {
Bitmap bm = ((BitmapDrawable)d).getBitmap();
//Maybe more code here?
lblImages.setImageBitmap(bm);
}*/
//lblImages.setImageResource(images);
//lblImages.DisplayImage(images);
//lblImages.DisplayImage(images);
}
}
怠惰なアダプター:
`package com.example.androidhive;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, int listRow, String[] strings, int[] is) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.TAG_TITLE));
artist.setText(song.get(CustomizedListView.TAG_MESSAGE));
duration.setText(song.get(CustomizedListView.TAG_DATE));
imageLoader.DisplayImage(song.get(CustomizedListView.TAG_IMAGES), thumb_image);
return vi;
}
}`