2016-07-27 12 views
1

セルをクリックすると、オブジェクトが配列に追加されます。この配列は別のアクティビティに渡す必要があります。テスト目的のために、私は1つだけのオブジェクト、DoctorObjectを持つ配列を渡しました。次に、次のアクティビティでは、.getName()を使用して文字列として名前を取得し、それをトーストで表示します。しかし、トーストは空です。カスタムオブジェクトのarraylistを別のアクティビティに渡す

public class DoctorObject implements Parcelable { 

private List<Object> mChildrenList; 

public DoctorObject(String name) { 
    Docname = name; 

} 

public DoctorObject (Parcel in){ 
    this.DocPic = in.readInt(); 
    this.Docname= in.readString(); 
    this.Docspecialty = in.readString(); 
    this.DocLocation = in.readString(); 
} 

String Docname = ""; 

public String getDocname() { 
    return Docname; 
} 

public void setDocname(String docname) { 
    Docname = docname; 
} 

public int getDocPic() { 
    return DocPic; 
} 

public void setDocPic(int docPic) { 
    DocPic = docPic; 
} 

public String getDocspecialty() { 
    return Docspecialty; 
} 

public void setDocspecialty(String docspecialty) { 
    Docspecialty = docspecialty; 
} 

public String getDocLocation() { 
    return DocLocation; 
} 

public void setDocLocation(String docLocation) { 
    DocLocation = docLocation; 
} 

int DocPic; 
String Docspecialty = ""; 
String DocLocation = ""; 





@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 

} 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public DoctorObject createFromParcel(Parcel in) { 
     return new DoctorObject(in); 
    } 

    public DoctorObject[] newArray(int size) { 
     return new DoctorObject[size]; 
    } 
}; 

アクティビティー:

ArrayList<DoctorObject> selectedItems = new ArrayList<>(); 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_my_team); 

    context = this; 


    /* Regular Expand listview 

    */ 

    // get the listview 
    expListView = (ExpandableListView) findViewById(R.id.newTeamListView); 

    // preparing list data 
    prepareListData(); 

    listAdapter = new ExpandableListAdapter(this, listDataDoc, listDataChildStaff); 

    // setting list adapter 
    expListView.setAdapter(listAdapter); 

    // expListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 

    // Listview on child click listener 
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      // TODO Auto-generated method stub 



      DoctorObject currentDoc = listDataDoc.get(groupPosition); 

      StaffObject currentStaff = listDataChildStaff.get(listDataDoc.get(groupPosition)).get(childPosition); 


      selectedItems.add(currentDoc); 

      String toastname = currentDoc.getDocname(); 
      String toastStaff = currentStaff.getStaffname(); 

      Toast.makeText(getApplicationContext(), 
        toastStaff, 
          Toast.LENGTH_SHORT).show(); 

      return true; 
     } 
    }); 



    /* End of regular expand listview 

    */ 



    nextButton = (Button) findViewById(R.id.nextbuttonTeam); 

    nextButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent myIntent = new Intent(CreateMyTeamActivity.this, CreateReferralTeamActivity.class); 
      myIntent.putParcelableArrayListExtra("NAME", selectedItems); 
      //myIntent.putExtra("Selections",selectedItems); 
      CreateMyTeamActivity.this.startActivity(myIntent); 


     } 
    }); 


} 

次のアクティビティ:ここ

コード

オブジェクトの一部抜粋である

public class CreateReferralTeamActivity extends AppCompatActivity { 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_referral_team); 

    Intent b = getIntent(); 
    ArrayList<DoctorObject> list = (ArrayList<DoctorObject>) b.getSerializableExtra("NAME"); 

    String lists =""; 
    for (int i=0; i < list.size(); i++){ 
     lists = list.get(i).getDocname(); 
    } 



    //TextView text = (TextView) findViewById(R.id.sampleText); 

    //text.setText(lists); 

    Toast.makeText(getApplicationContext(), 
      lists, 
      Toast.LENGTH_SHORT).show(); 

} 

}

これを実行すると、アレイが空であるように見えます。

http://imgur.com/a/EGgL6

答えて

1

あなたがいることを、DoctorObject#writeToParcel()空を持つことができませんオブジェクトデータを実際にコピーするものです。

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    // the order you write to the Parcel has to be the same order you read from the Parcel 
    dest.writeInt(DocPic); 
    dest.writeString(Docname); 
    dest.writeString(Docspecialty); 
    dest.writeString(DocLocation); 
} 

そして、他の活動にあなたのデータをリードバックするために、あなたはTオブジェクトParcelableオブジェクトでない場合は、ケース内の別のアクティビティへのArrayListを渡すGsonライブラリを使用することができますgetParcelableArrayListExtra

ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME"); 
1

利用getParcelableArrayListExtraの代わりgetSerializableExtraputParcelableArrayListExtraを使用して、以前の活動からのArrayListオブジェクトを送信するために:ここで

は、アニメーションGIFはある

ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME"); 
+0

私はちょうどそれを試み、それでも動作しません。私はまだ空白のトーストを手に入れます。 – Adilp

+0

@Adilp:CreateReferralTeamActivityアクティビティでLog.i( "TAG"、 "list SIZE:" + list.size()) 'を使用し、ArrayListのサイズを確認してください。 –

+0

07-27 09:05:33.547 4876-4876/com。 adilpatel.vitalengine I/TAG:リストサイズ:1 – Adilp

2

を使用。ジェネリックタイプはどこにありますか? JSON文字列へのArrayListに変換して渡す:あなたのGradleの依存関係に

dependencies { 
    compile 'com.google.code.gson:gson:2.7' 
} 

ステップ2をこの行を追加します。最初のインポートGsonライブラリのGradleスクリプト

を使用して:あなたのケースでは、

ステップ1のArrayListです活動1から活動へのJSON文字列2

 ArrayList<DoctorObject> doctors = new ArrayList<Location>(); 
     doctors.add(new DoctorObject(params)); 
     doctors.add(new DoctorObject(params)); 

     Gson gson = new Gson(); 
     String jsonString = gson.toJson(doctors); 
     Intent intent = new Intent(MainActivity.this,MapsActivity.class); 
     intent.putExtra("KEY",jsonString); 
     startActivity(intent); 

ステップ3:バンドルからJSON文字列を取得し、バック

0 ArrayListにそれを変換
import java.lang.reflect.Type; 


    Bundle bundle = getIntent().getExtras(); 
    String jsonString = bundle.getString("KEY"); 

    Gson gson = new Gson(); 
    Type listOfdoctorType = new TypeToken<List<DoctorObject>>() {}.getType(); 
    ArrayList<DoctorObject> doctors = gson.fromJson(jsonString,listOfdoctorType); 
関連する問題