我有一個onClickListener應該調用一個類,顯示和圖像從一個靜態的url。我在所謂的課堂上敬酒,並且表現出沒有形象。 list.onClickListener應該調用應該發佈圖像的類ShowImage。請幫助我不確定我在做什麼不正確。加載圖片從URL問題
注意*在我的main_layout.xml中我有一個imageview。
package com.flash_tattoo;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Bundle bundle = getIntent().getExtras();
String jsonData = bundle.getString("jsonData");
JSONArray jsonArray;
try {
jsonArray = new JSONArray(jsonData);
String[] mStrings = new String[jsonArray.length()];
String[] mImages = new String[jsonArray.length()];
String[] mPath = new String[jsonArray.length()];
for (int i=0; i<jsonArray.length(); i++)
{
String url = jsonArray.getJSONArray(i).getString(2);
mStrings[i] = "http://www.2020mediaonline.com/tattoo/thumbnail_image/" + url;
String image_name = jsonArray.getJSONArray(i).getString(1);
mImages[i] = image_name;
String path_name = jsonArray.getJSONArray(i).getString(1);
mPath[i] = path_name;
}
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, mImages);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
ShowImage go = new ShowImage();
Toast.makeText(MainActivity.this, "In onClick", Toast.LENGTH_LONG).show();
}
});
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(listener);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class ShowImage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("http://www.ansi.okstate.edu/breeds/other/llama/llama1.jpg");
imgView.setImageDrawable(drawable);
Toast.makeText(MainActivity.this, "show big image", Toast.LENGTH_LONG).show();
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
@Override
public void onDestroy()
{
adapter.imageLoader.stopThread();
list.setAdapter(null);
super.onDestroy();
}
public OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,flash_tattoo.class);
startActivityForResult(myIntent, 0);
}
};
}
說實話這是一個猜測,因爲我仍然是一個初學者,我以爲我是在正確的軌道上。我不確定我是如何稱呼該課程並顯示結果的。 – Denoteone