我下载了几个jpeg文件,并在ListView上的项目中显示它们。然后,当用户点击列表视图的项目时,我想在新的活动中再次显示相应的jpeg文件以及一些新的屏幕上的其他信息。我使用intent来启动新活动,但我不知道如何传递jpeg文件。有可能将它作为额外的意图传递给意图吗?
在我试图通过@geet实现下面显示的方法之后,这是包含开始第二个活动的意图的整个活动:
public class DisplayActivity extends Activity {
private MySQLiteHelper dbHelper;
private SQLiteDatabase db;
private ListView lv_custom;
ArrayList<Content> contents = new ArrayList<Content>();
Content content0 = new Content();
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
lv_custom = (ListView) findViewById(android.R.id.list);
dbHelper = new MySQLiteHelper(this);
db = dbHelper.getWritableDatabase();
LoadImgAsyncTask l = new LoadImgAsyncTask(contents);
l.execute();
}
private class Content {
private Drawable img;
private String phoneNr;
private String address;
}
private class ViewHolder{
ImageView image;
TextView textViewPhone;
TextView textViewAddress;
}
private class CustomListViewAdapter extends BaseAdapter{
private int layoutResource;
private ArrayList<Content> mArrayList;
private LayoutInflater inflater;
public CustomListViewAdapter(Context mContext, int layoutResource, ArrayList<Content> mArrayList) {
this.layoutResource = layoutResource;
this.mArrayList = mArrayList;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mArrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
convertView = inflater.inflate(layoutResource, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) convertView.findViewById(R.id.imageView1);
viewHolder.textViewPhone = (TextView) convertView.findViewById(R.id.phoneNr);
viewHolder.textViewAddress = (TextView) convertView.findViewById(R.id.address);
viewHolder.image.setImageDrawable(mArrayList.get(position).img);
viewHolder.textViewPhone.setText(mArrayList.get(position).phoneNr);
viewHolder.textViewAddress.setText(mArrayList.get(position).address);
return convertView;
}
}
private Drawable loadImageFromWeb(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;
}}
private class LoadImgAsyncTask extends AsyncTask<Void, Void, ArrayList<Content>>{
private ProgressDialog progressDialog;
private ArrayList<Content> cnt;
LoadImgAsyncTask(ArrayList<Content> cnt){
this.cnt = cnt;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(DisplayActivity.this);
progressDialog.setMessage("Downloading logos. Please wait some more...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected ArrayList<Content> doInBackground(Void ...voids ) {
String[] selectionArguments = {};
////////c = db.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_STORES + " GROUP BY " + MySQLiteHelper.COLUMN_NAME + " HAVING COUNT(" + MySQLiteHelper.COLUMN_NAME +") < 2", selectionArguments);
c = db.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_STORES, selectionArguments);
if (c.getCount() != 0) {
try {
while (c.moveToNext()) {
content0 = new Content();
//add a new store information to the ArrayList
content0.img = loadImageFromWeb(c.getString(6));
content0.phoneNr = c.getString(7);
content0.address = c.getString(1);
contents.add(content0);
}
} catch (Exception e) {
Log.v("Exception e ",e.fillInStackTrace().toString());
}}
else {
Log.v("Cursor ", 0 + "");
Toast.makeText(getApplicationContext(),
"Unfortunately the connection is bad. Try again later.",
Toast.LENGTH_SHORT).show();
}
db.close();
return contents;
}
@Override
protected void onPostExecute(ArrayList<Content> cnt) {
super.onPostExecute(cnt);
progressDialog.hide();
progressDialog.dismiss();
CustomListViewAdapter clva = new CustomListViewAdapter(DisplayActivity.this, R.layout.one_row, cnt);
lv_custom.setAdapter(clva);
lv_custom.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
c.moveToPosition(position);
String adr = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ADDRESS));
String city = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_CITY));
String name = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
String store_id = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STORE_ID));
String phone = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_PHONE));
String zip = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ZIP));
String state = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STATE));
Double latitude = c.getDouble(c.getColumnIndex(MySQLiteHelper.COLUMN_LAT));
Double longitude = c.getDouble(c.getColumnIndex(MySQLiteHelper.COLUMN_LONG));
Intent intent=new Intent(DisplayActivity.this,DisplayInfoActivity.class);
intent.putExtra("Address",adr);
intent.putExtra("City", city);
intent.putExtra("Zip", " " + zip + ", ");
intent.putExtra("State", state);
intent.putExtra("Name", "Store name: " + name);
intent.putExtra("StoreID", "Store ID: " + store_id);
intent.putExtra("Phone", "Phone number: " + phone);
intent.putExtra("Latitude", "Latitude=" + latitude);
intent.putExtra("Longitude", "Longitude=" + longitude);
final ImageView imageView = (ImageView)v.findViewById(R.id.imageView1);
final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
final Bitmap myBitmap = bitmapDrawable.getBitmap();
intent.putExtra("BitmapImage", myBitmap);
startActivity(intent);
}
});
}
}
}
这是第二项活动:
public class DisplayInfoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView logo = (ImageView) findViewById(R.id.imageView2);
setContentView(R.layout.list_entry);
TextView address = (TextView) findViewById(R.id.address_entry);
TextView city = (TextView) findViewById(R.id.city_entry);
TextView zip = (TextView) findViewById(R.id.zip_entry);
TextView state = (TextView) findViewById(R.id.state_entry);
TextView name = (TextView) findViewById(R.id.name_entry);
TextView store_id = (TextView) findViewById(R.id.store_id_entry);
TextView phone = (TextView) findViewById(R.id.number_entry);
TextView latitude = (TextView) findViewById(R.id.latitude_entry);
TextView longitude = (TextView) findViewById(R.id.longitude_entry);
Intent intent = getIntent();
Bitmap img = (Bitmap) intent.getParcelableExtra("BitmapImage");
if (logo == null)
Log.v("MY TAG ","logo is null");
else Log.v("MY TAG ","logo is NOT null");
logo.setImageBitmap(img);
address.setText(intent.getStringExtra("Address"));
city.setText(intent.getStringExtra("City"));
zip.setText(intent.getStringExtra("Zip"));
state.setText(intent.getStringExtra("State"));
name.setText(intent.getStringExtra("Name"));
store_id.setText(intent.getStringExtra("StoreID"));
phone.setText(intent.getStringExtra("Phone"));
latitude.setText(intent.getStringExtra("Latitude"));
longitude.setText(intent.getStringExtra("Longitude"));
}
}
这是来自LogCat的片段:
12-02 20:01:04.854: V/MY TAG(23878): logo is null
12-02 20:01:04.854: D/AndroidRuntime(23878): Shutting down VM
12-02 20:01:04.854: W/dalvikvm(23878): threadid=1: thread exiting with uncaught exception (group=0x419a32a0)
12-02 20:01:04.854: E/AndroidRuntime(23878): FATAL EXCEPTION: main
12-02 20:01:04.854: E/AndroidRuntime(23878): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottlerocket/com.example.bottlerocket.DisplayInfoActivity}: java.lang.NullPointerException
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread.access$700(ActivityThread.java:134)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.os.Looper.loop(Looper.java:137)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread.main(ActivityThread.java:4867)
12-02 20:01:04.854: E/AndroidRuntime(23878): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:01:04.854: E/AndroidRuntime(23878): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 20:01:04.854: E/AndroidRuntime(23878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-02 20:01:04.854: E/AndroidRuntime(23878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-02 20:01:04.854: E/AndroidRuntime(23878): at dalvik.system.NativeStart.main(Native Method)
12-02 20:01:04.854: E/AndroidRuntime(23878): Caused by: java.lang.NullPointerException
12-02 20:01:04.854: E/AndroidRuntime(23878): at com.example.bottlerocket.DisplayInfoActivity.onCreate(DisplayInfoActivity.java:33)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.Activity.performCreate(Activity.java:5047)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-02 20:01:04.854: E/AndroidRuntime(23878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
12-02 20:01:04.854: E/AndroidRuntime(23878): ... 11 more
答案 0 :(得分:2)
是的,可能。
你可以在列表视图的onclick事件中传递它。
final ImageView imageView = (ImageView) view.findViewById(R.id.idOfImageviewInListview);
final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
final Bitmap yourBitmap = bitmapDrawable.getBitmap();
Intent intent = new Intent(currentclass.this, Secondclass.class);
intent.putExtra("BitmapImage", yourBitmap);
in Secondclass:
Intent intent = getIntent();
Bitmap img = (Bitmap) intent.getParcelableExtra("BitmapImage");
编辑:
请更改此行
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
要
final ImageView imageView = (ImageView)v.findViewById(R.id.imageView1);
编辑: 在设置视图之前,无法获取当前视图的对象。
更改
super.onCreate(savedInstanceState);
ImageView logo = (ImageView) findViewById(R.id.imageView2);
setContentView(R.layout.list_entry);
要:
super.onCreate(savedInstanceState);
setContentView(R.layout.list_entry);
ImageView logo = (ImageView) findViewById(R.id.imageView2);
答案 1 :(得分:1)
此:
ImageView logo = (ImageView) findViewById(R.id.imageView2);
必须在此之后:
setContentView(R.layout.list_entry);
答案 2 :(得分:0)
您还可以使用意图将图像路径传递给第二个活动。