您好我在Android应用程序中开发了一个xml解析器。这是在listview(产品列表)中成功解析xml。现在我已经实现了这个活动,意图是另一个活动。
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
String Name = ((TextView) view.findViewById(R.id.title)).getText().toString();
String Category = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String Price = ((TextView) view.findViewById(R.id.duration)).getText().toString();
String Image=((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, Name);
in.putExtra(KEY_ARTIST, Category);
in.putExtra(KEY_THUMB_URL, Image);
in.putExtra(KEY_DURATION, Price);
startActivity(in);
下一个活动目的是用户点击产品列表中的一个特定产品,下次活动必须显示特定产品详细信息。
nextactivity是SingleMenuLietItem.java源代码是
package com.example.truebranches;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_TITLE = "Name";
static final String KEY_ARTIST = "Category";
static final String KEY_THUMB_URL = "Image";
static final String KEY_DURATION = "Price";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String Name = in.getStringExtra(KEY_TITLE);
String ProductURL = in.getStringExtra(KEY_ARTIST);
String Image = in.getStringExtra(KEY_THUMB_URL);
String Price = in.getStringExtra(KEY_DURATION);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblProductURL = (TextView) findViewById(R.id.producturl_label);
ImageView lblImage = (ImageView) findViewById(R.id.image_label);
TextView lblCost = (TextView) findViewById(R.id.price_label);
lblName.setText(Name);
lblProductURL.setText(ProductURL);
lblImage.setImageResource(Image);
lblCost.setText(Price);
}
}
这里下一个活动必须显示textview产品详细信息..但是图像没有显示..因为我在这部分挣扎...所以我必须为图像更改哪一行将在下一个活动中显示。这里
lblImage.setImageResourse(Image)dis line有错误类型ImageView中的方法setImageResource(int)不适用于参数(String)...如何清除它。请帮我。
答案 0 :(得分:0)
您只是设置图像就像设置列表项图像一样吗?
我认为它不会因此而起作用:
String Image=((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();
更新: 使用此方法下载图像:
public void downloadImage(String url, String pictureName) {
final HttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
String packageName = Environment.getExternalStorageDirectory() + "/"
+ getString(R.string.app_name);
folder = new File(packageName);
folder.mkdirs();
File f = new File(folder, pictureName);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
// Bug on slow connections, fixed in future release.
Bitmap bm = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
try {
FileOutputStream out = new FileOutputStream(f.getAbsolutePath());
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
bm.recycle();
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w("ImageDownload", "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w("ImageDownload", "Incorrect URL: " + url);
} catch (Exception e) {
getRequest.abort();
Log.w("ImageDownload", "Error while retrieving bitmap from " + url, e);
} finally {
}
}
并在您的SingleMenuItemActivity中:
String packageName = Environment.getExternalStorageDirectory() + "/"
+ getString(R.string.app_name);
folder = new File(packageName);
folder.mkdirs();
File f = new File(folder, pictureName);
Bitmap bitmap = null;
if (f.isFile())
try {
bitmap = BitmapFactory.decodeStream(f.toURL().openStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap != null)
lblImage.setImageBitmap(bitmap);