Problem with Parcelable

Put your problem here if it does not fit any of the other categories.

Problem with Parcelable

Postby bharath » Mon Dec 08, 2008 5:32 am

Hi All,

1)How to pass ArrayList <which takes user defined class as a parameter> from one activity to another using Parcelable ?
Below is my code,

Code: Select all
public class GroupTemplateForm extends Activity  {
   
   private static ArrayList<GroupData> grNamelist = new ArrayList<GroupData>();
      
   public static class GroupData implements Parcelable {
      
       private String grname,grdesc;
      
        public void writeToParcel(Parcel out, int flags) {
           [b]out.writeList((List<GroupData>)grNamelist);[/b]        }
        
        public  static final Parcelable.Creator<GroupData> CREATOR =
            new Parcelable.Creator<GroupData>() {
            public GroupData createFromParcel(Parcel in) {
              // in.readList(outVal, loader)
                return new GroupData("android","android");
            }

         @Override
         public GroupData[] newArray(int size) {
            return new GroupData[size];
         }
        };
        
       GroupData(String name,String desc){
          grname=name;
          grdesc=desc;
       }
      
       public String getName(){
          return grname;
       }
      
       public String getDesc(){
          return grdesc;
       }
        
      @Override
      public int describeContents() {
         return 0;
      }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupform); 
    }
   
    public boolean checkGroupList(){
       return true;
    }
   
    public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
      menu.add(0,0,0,"Save&New");
      menu.add(0,1,1,"Save&Exit");
      return true;
   }
   
    @Override
   public boolean onMenuItemSelected(int featureId, MenuItem item) {
       switch(item.getItemId()){
       case 0:
          EditText grname = (EditText) findViewById(R.id.gr_name);
          EditText grdes = (EditText) findViewById(R.id.gr_des);
          grNamelist.add(new GroupData(grname.toString(),grdes.toString()));
          grname.setText("");
          grdes.setText("");
          break;
       case 1:
          EditText grname1 = (EditText) findViewById(R.id.gr_name);
          EditText grdes1 = (EditText) findViewById(R.id.gr_des);
         Intent i=getIntent();
         //i.putParcelableArrayListExtra("GroupName", new ArrayList<new MyParcelable(new GroupData())>());
         [b]i.putParcelableArrayListExtra("com.vista.examples.List.Group", grNamelist) ;   [/b]      i.putExtra("GroupName", grname1.getText().toString());
         i.putExtra("GroupDes", grdes1.getText().toString());
         GroupTemplateForm.this.setResult(1,i);
         GroupTemplateForm.this.finish();
         break;
       }
      return super.onMenuItemSelected(featureId, item);
       
   }
}


i am getting error at out.writeList((List<GroupData>)grNamelist) . which method i need to use to read arraylist<groupdata> from the parcel?
Regards
Bharat.
bharath
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Sep 27, 2008 11:21 am

Top

Postby MrSnowflake » Mon Dec 08, 2008 10:07 am

[mod]I guess the tripple post was an accident, deleted them.

You don't have to cast an ArrayList<?> to List<?> as ArrayList implements List. I can write my arraylists to parcel without problem.

I think you have Parcelable implemented at the wrong level! You should create a datatype with the ArrayList<GroupData> and have that implement Parcelable, as you now write an arraylist, from a datatype which doesn't have an ArrayList.

I'll give an example using a simplified version of a Parcelable I use:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class Message implements Parcelable {
  2. ArrayList<String> receivers;
  3. String message;
  4. //...
  5. public void writeToParcel(Parcel out, int flags) {
  6.   out.writeString(message);
  7.   out.writeStringList(receivers);
  8. }
  9. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
This works!
Another thing you should do: Objects you want to write to parcel need to conform to the rules in Parcel.writeValue(), so your object needs to implement Serializable!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Re:

Postby bharath » Wed Dec 10, 2008 6:04 am

Hi MrSnowflake
Can u give an example code like how to pass ArrayList <which takes user defined class as a parameter> from one activity to another using Parcelable ?

Regards
Bharat.
bharath
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Sep 27, 2008 11:21 am

Postby MrSnowflake » Wed Dec 10, 2008 11:59 am

I told you: make the user defined class serializable!

[mod]And this is NOT a tutorial!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests