リストビューを使用してURLから画像を取得する場合、キャッシュを削除して画像を表示するコードでImageLoaderクラスを使用しました。私のコードを参照してください。ロードするアダプタークラスをfifthstep class
使用しますCategoryListAdapter2
imageLoader.DisplayImage(fifthstep.Category_image.get(position), activity, holder.imgThumb);
キャッシュを削除してimageviewに画像を表示しますが、以下の3番目のクラスでは、listviewを直接使用してurlからjsonデータを解析しません。そのため、エラーが発生したため、画像が表示されないことがあり、サードクラスでローダークラスを使用するにはどうすればよいですか?? この例 も使用されていますMeoryキャッシュImageLoaderクラスリストビューを使用しない3番目のクラスでtis関数を使用するにはどうすればよいですか
public class fifthstep extends Activity {
private HorizontalListView2 listview;
CategoryListAdapter2 cla;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
String CategoryAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenulist);
cla = new CategoryListAdapter2(fifthstep.this);
CategoryAPI = Utils.CategoryAPI2;
// clearData();
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(CategoryAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new
InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("worldpopulation");
// // JSONArray results = json.getJSONArray("worldpopulation2");
JSONObject person=json.getJSONObject("worldpopulation2");
String Name=person.getString("describtion");
String url2=person.getString("url");
txtdescription.setText(Name);
Bitmap bitmap =
BitmapFactory.decodeStream((InputStream)new URL(url2).getContent());
imageView1.setImageBitmap(bitmap);
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
// JSONObject category =
object.getJSONObject("Category");
Category_ID.add(Long.parseLong(object.getString("rank")));
Category_name.add(object.getString("name"));
Category_image.add(object.getString("url"));
Log.d("Category name", Category_name.get(i));
listview.setAdapter(cla);
}
public class CategoryListAdapter2 extends BaseAdapter {
private Activity activity;
private ImageLoader imageLoader;
public CategoryListAdapter2(Activity act) {
this.activity = act;
imageLoader = new ImageLoader(act);
}
public int getCount() {
// TODO Auto-generated method stub
return fifthstep.Category_ID.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.viewitem, null);
holder = new ViewHolder();
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtText = (TextView) convertView.findViewById(R.id.title);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.image);
holder.txtText.setText(fifthstep.Category_name.get(position));
imageLoader.DisplayImage(fifthstep.Category_image.get(position),
activity, holder.imgThumb);
return convertView;
}
static class ViewHolder {
TextView txtText;
ImageView imgThumb;
}
//is working perfect every time display iamge from url//////////////
public class thirdstep extends Activity {
String MenuSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list2);
ImageView img1 = (ImageView) findViewById(R.id.img1);
TextView txt1 = (TextView) findViewById(R.id.txt1);
ImageView img2 = (ImageView) findViewById(R.id.img2);
TextView txt2 = (TextView) findViewById(R.id.txt2);
ImageView img3 = (ImageView) findViewById(R.id.img3);
TextView txt3 = (TextView) findViewById(R.id.txt3);
MenuSelect = Utils.MenuSelect;
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(MenuSelect);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null) {
str += line;
}
JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("worldpopulation");
JSONObject jsonOrder = (JSONObject)data.get(0);
txt1.setText (jsonOrder.getString("name"));
String url1 = jsonOrder.getString("url");
//
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(url1).getContent());
img1.setImageBitmap(bitmap);
// img1.setImageBitmap( /* decoded url input stream*/);
JSONObject jsonOrder2 = (JSONObject)data.get(1);
txt2.setText (jsonOrder2.getString("name2"));
String url2 = jsonOrder2.getString("url2");
//
Bitmap bitmap2 = BitmapFactory
.decodeStream((InputStream) new URL(url2).getContent());
img2.setImageBitmap(bitmap2);
// img2.setImageBitmap( /* decoded url input stream*/);
JSONObject jsonOrder3 = (JSONObject)data.get(2);
txt3.setText (jsonOrder3.getString("name3"));
String url3 = jsonOrder3.getString("url3");
//
Bitmap bitmap3 = BitmapFactory
.decodeStream((InputStream) new URL(url3).getContent());
img3.setImageBitmap(bitmap3);