【发布时间】:2012-09-16 20:53:28
【问题描述】:
将图像从一个活动发送到另一个活动但能够将文本发送到另一个活动的问题,放置一些带有文件名的源代码请查看并更正错误,如果可能的话,请写下所需的代码,因为我面临这个问题最近两天,我在stackoverflow中提交了这个问题3次,但我没有得到很好的答案,请有人写下我需要做的正确事情并为我编写所需的代码,但这里我放了一些我的代码:
MainActivity 代码:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById
(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById
(R.id.artist)).getText().toString();
String duration = ((TextView) view.findViewById
(R.id.duration)).getText().toString();
byte[] image = null;
Bitmap thumb_url = BitmapFactory.decodeByteArray
(image, 0, image.length);
// Bitmap bMap = BitmapFactory.decodeByteArray
(array, 0, array.length);
// Starting new intent
Intent in = new Intent
(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_DURATION, duration);
in.putExtra(KEY_THUMB_URL, thumb_url);
startActivity(in);
}
});
}
}
接收活动代码:
public class SingleMenuItemActivity extends Activity {
// XML node keys
private static final String KEY_TITLE = "title";
private static final String KEY_ARTIST = "artist";
private static final String KEY_DURATION = "duration";
private static final String KEY_THUMB_URL = "thumb_url";
@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 title = in.getStringExtra(KEY_TITLE);
String artist = in.getStringExtra(KEY_ARTIST);
String duration = in.getStringExtra(KEY_DURATION);
ImageView image = (ImageView)findViewById(R.id.thumb_url);
Object thumb_url = null;
View bitmap = null;
bitmap.setBackgroundResource((Integer) thumb_url);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
//what code to write for image here
lblName.setText(title);
lblCost.setText(artist);
lblDesc.setText(duration);
}
}
XML 文件代码:
<ImageView
android:id="@+id/thumb_url"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/rihanna"/>
<TextView android:id="@+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<TextView android:id="@+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
<TextView android:id="@+id/mobile_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>
【问题讨论】:
-
你可以从intent中获取图片。
标签: android android-intent android-emulator android-listview android-activity