发送图片ID从片段到另一个活动
问题描述:
大家好我有一个问题,使用从片段意图发送图片ID到另一个活动,我搜索了有关问题,我看到了意图发送ID为双不int整理任何想法如何分享图片活动之间的ID。此问题与我的问题相同,但解决方案对我无效:How can i get Image Resource ID and send it to other activity in Android?。 这里是我的程序发送图片ID从片段到另一个活动
public class BestTenPlaces extends Fragment {
public BestTenPlaces() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list, container, false);
final View detailsView = inflater.inflate(R.layout.details, container, false);
ArrayList<InfoObject> objects = new ArrayList<InfoObject>();
objects.add(new InfoObject("baghdad 1", "gitnge", R.drawable.capture));
objects.add(new InfoObject("string 2", "gitnge", R.drawable.capture));
objects.add(new InfoObject("string 3", "gitnge", R.drawable.capture));
objects.add(new InfoObject("string 4", "gitnge", R.drawable.capture));
objects.add(new InfoObject("string 5", "gitnge", R.drawable.capture));
objects.add(new InfoObject("string 6", "gitnge", R.drawable.capture));
final ArrayList<InfoObject> details = new ArrayList<InfoObject>();
details.add(new InfoObject("string 1", R.drawable.capture));
details.add(new InfoObject("string 2", R.drawable.capture));
details.add(new InfoObject("string 3", R.drawable.capture));
details.add(new InfoObject("string 4", R.drawable.capture));
details.add(new InfoObject("string 5", R.drawable.capture));
details.add(new InfoObject("string 6", R.drawable.capture));
GeneralAdapter BestPlaces = new GeneralAdapter(getActivity(), objects);
ListView BestPlacesList = (ListView) rootView.findViewById(R.id.list);
BestPlacesList.setAdapter(BestPlaces);
BestPlacesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
InfoObject detailsArray = details.get(position);
String m = detailsArray.getmTextDetails();
int s = (int) detailsArray.getImageResourcesObject();
Intent intent = new Intent(getActivity(), details.class);
intent.putExtra("message", m);
intent.putExtra("resource", s);
startActivity(intent);
}
});
return rootView;
}
}
,这里是活动
public class details extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
Bundle bundle = getIntent().getExtras();
Bundle extra = getIntent().getExtras();
int resources = extra.getInt("resource");
String message = bundle.getString("message");
TextView textDetails = (TextView) findViewById(R.id.text_details);
textDetails.setText(message);
ImageView imageDetails = (ImageView) findViewById(R.id.image_details);
imageDetails.setImageResource(resources);
}
}
这里的遍历infoObject
public class InfoObject {
private String locationObject;
private String placeNameObject;
private int imageResourcesObject;
private String mTextDetails;
private int mDetailsImageResource;
public InfoObject(String location, String placeName, int imageResources) {
locationObject = location;
placeNameObject = placeName;
imageResourcesObject = imageResources;
}
public InfoObject(String textDetails, int detailsImageResource) {
mTextDetails = textDetails;
mDetailsImageResource = detailsImageResource;
}
public String getmTextDetails() {
return mTextDetails;
}
public int getmDetailsImageResource() {
return mDetailsImageResource;
}
public String getLocationObject() {
return locationObject;
}
public String getPlaceNameObject() {
return placeNameObject;
}
public int getImageResourcesObject() {
return imageResourcesObject;
}
public void setLocationObject(String locationObject) {
this.locationObject = locationObject;
}
public void setPlaceNameObject(String placeNameObject) {
this.placeNameObject = placeNameObject;
}
public void setImageResourcesObject(int imageResourcesObject) {
this.imageResourcesObject = imageResourcesObject;
}
}
答
当你填充阵列details
在onCreateView
方法,你使用的InfoObject
下面的构造:
new InfoObject("string 3", R.drawable.capture)
此构造函数初始化场mDetailsImageResource
NOT imageResourcesObject
您稍后再试从读。
由于Java中未初始化int
类成员将当您尝试从imageResourcesObject
这是0
阅读被自动初始化为0
。
+0
非常感谢你 –
答
简化您的方法,他们会更有效。例如,让我们传递消息。基本上,你会从一个片段发送一个字符串到一个新的活动。
在片段,请致电:
intent.putExtra("message", detailsArray.getmTextDetails());
然后在活动:
String message = getIntent().getStringExtra().getString("message");
答
使用此而不是详细活动
int resources = getIntent().getIntExtra("resource", 0);
String message = getIntent().getString("message");
希望这将有助于。
答
您可以通过适当的方式做到这一点:
- 使有方法可以从片段调用一个接口。
- 在您的活动中实现此接口
- 从您的片段中创建接口的对象
onAttach()
给出活动上下文的方法。 - 借助于这个对象
onActivityCreated()
调用你的方法。
你是否在'details'活动中获得''message“'('textDetails' TextView)? –
是的我在“textDetails”TextView中得到的消息,但我没有得到图像Id –
请发布'InfoObject'的源代码。 –