将图片ID从片段发送到另一个活动

时间:2017-10-14 07:40:47

标签: android android-fragments

大家好我从使用片段到另一个活动的意图发送图片ID有问题,我搜索了我看到的问题意图发送id为double而不是任何想法如何在活动之间共享图片ID。这个问题与我的问题相同,但解决方案对我不起作用:How can i get Image Resource ID and send it to other activity in Android?。 这是我的计划

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;
}
}

4 个答案:

答案 0 :(得分:0)

简化您的方法,它们会更有效。 例如,让我们传递消息。基本上,您将从片段向一个新活动发送一个字符串。

在片段中,请致电:

let sinchUserID:String = userID as String

    self.sinchClient = Sinch.client(withApplicationKey: SINCH_APPLICATION_KEY, applicationSecret: SINCH_APPLICATION_SECRET, environmentHost: "sandbox.sinch.com", userId: sinchUserID)

    self.sinchClient!.setSupportMessaging(true)

    self.sinchClient!.enableManagedPushNotifications()
    // or
    self.sinchClient!.setSupportActiveConnectionInBackground(true)


    self.sinchClient!.delegate = self

    self.sinchClient!.start()

    self.sinchClient!.startListeningOnActiveConnection()

    self.sinchClient?.setPushNotificationDisplayName(ALERT_TITLE)


    self.push = Sinch.managedPush(with: SINAPSEnvironment.development)
    self.push?.delegate = self
    self.push?.setDesiredPushTypeAutomatically()


    self.push?.setDisplayName(UserDefaults.standard.value(forKey: "userFirstname") as! String)

    self.push?.setDesiredPushType(SINPushTypeRemote)
    self.push?.registerUserNotificationSettings()

    if UserDefaults.standard.integer(forKey: "newMessageStatus") == 0
    {
        self.sinchClient?.registerPushNotificationDeviceToken(UserDefaults.standard.value(forKey: "deviceTokenForSinch") as! Data, type: SINPushTypeRemote, apsEnvironment: SINAPSEnvironment.development)
    }
    else
    {

        self.sinchClient?.unregisterPushNotificationDeviceToken()
    }

然后在活动中:

intent.putExtra("message", detailsArray.getmTextDetails());

答案 1 :(得分:0)

在“详情活动”

中使用此功能
  WHERE fieldA = 'abcde' AND fieldB IN ('0002','0003')

希望这会有所帮助。

答案 2 :(得分:0)

你可以通过适当的方式做到这一点:

  1. 制作一个具有可以从片段调用的方法的接口。
  2. 在您的活动中实施此界面
  3. 来自您的onAttach()方法中的片段make interface对象,它提供了活动上下文。
  4. 借助此对象onActivityCreated()调用您的方法。

答案 3 :(得分:0)

当您在details方法中填充数组onCreateView时,您使用的是InfoObject的以下构造函数:

new InfoObject("string 3", R.drawable.capture)

此构造函数初始化您稍后尝试读取的字段mDetailsImageResource NOT imageResourcesObject

由于Java中未初始化的int类成员会自动初始化为0,当您尝试从imageResourcesObject读取时0