2017-10-14 24 views
-3

こんにちは皆、フラグメントから別のアクティビティへのインテントを使用して画像IDを送信する際に問題があります。私はそのインテントを見て問題を検索しました。アクティビティ間の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); 
} 
} 

はここインフォ

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

'' details'アクティビティで ''メッセージ ''( 'textDetails' TextView)をうまく使っていますか? –

+0

はい "textDetails" TextViewにメッセージが表示されますが、画像IDを取得しません –

+0

'InfoObject'のソースコードを投稿してください。 –

答えて

0

あなたはonCreateView方法で配列detailsを移入しているとき、あなたはInfoObjectの次のコンストラクタを使用している:

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

このコンストラクタは、あなたが後でから読み取ろうフィールドmDetailsImageResourceませimageResourcesObjectを初期化。あなたはそれが0imageResourcesObjectから読み取るしようとすると、Javaで初期化されていないintクラスメンバー以来

は、自動的に0に初期化されます。

+0

非常にありがとう –

0

ですされていますあなたの方法を簡素化し、彼らがより効果的になります。 たとえば、メッセージを渡します。基本的には、フラグメントから新しいアクティビティに文字列を送ることになります。フラグメントで

、呼び出し:

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

をその後の活動に:

String message = getIntent().getStringExtra().getString("message"); 
0

使用この代わりに、詳細活動における

int resources = getIntent().getIntExtra("resource", 0); 
String message = getIntent().getString("message"); 

を助けることを願っています。

0

あなたは適切な方法でこれを行うことができます。

  1. をフラグメントから呼び出すことができるメソッドを持つインターフェイスを1つ作ります。
  2. あなたのアクティビティでは、このインターフェイスを実装します
  3. あなたのフラグメントは、あなたのonAttach()メソッドで、アクティビティコンテキストを与えるメソッドのオブジェクトを作成します。
  4. onActivityCreated()このオブジェクトの助けを借りて、あなたのメソッドを呼び出してください。
関連する問題