0

I have a class named register_information which looks like

// getting name
public String get_name()
{
    return this.name;
}
// setting name
public void set_name(String name)
{
    this.name=name;
}

I used some code to get all of the name and password which is registered from MainActivity like this.

List<register_information> all_reg_info_lists = db.get_all_info(); 

            List<HashMap<String, String>> list_of_reg_info = new ArrayList<HashMap<String, String>>();

              for (register_information reg_info : all_reg_info_lists)
              {                 
                  HashMap<String, String> hm = new HashMap<String, String>();
                   String name = reg_info.get_name();
                    String password=reg_info.get_password() ;

                      hm.put("key_name",name);
                      hm.put("key_password",password);

                      list_of_reg_info.add(hm);
                   }

Now i want to pass these all name and password into another window for showing them as list-view by intent. I used intent like this

Intent intObj = new Intent(MainActivity.this,showData_in_textView_class.class);         
            intObj.putExtra("data", list_of_reg_info);
            startActivity(intObj);

In my showData_in_textView_class i received the intent like this

public class showData_in_textView_class extends Activity{

   ArrayAdapter adapter;
   List<register_information> reg_info_lists; 

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_xml_page);


        Intent intename = getIntent();
        reg_info_lists= (List)intename.getSerializableExtra("data");

} 

}

But it didn't work. How can i do that??

ImonBayazid
  • 1,066
  • 3
  • 16
  • 41
  • http://stackoverflow.com/questions/17629379/pass-the-arraylist-with-hashmap-between-activities-in-android/17629519#17629519. check this similar. – Raghunandan Jul 19 '13 at 09:56

3 Answers3

1

You can pass a serializable variable with an Intent. Instead of List which is not serializable, simply use ArrayList which is.

Sean
  • 5,176
  • 2
  • 34
  • 50
0

First of all you are putting in the intent a List<HashMap<String, String>> and you are reading it back as List<register_information>

I usually use Gson for such purposes, the google library for json serialization/deserialization. It's very simple to use, in your case the code of intent creation should be something like this

Intent intObj = new Intent(MainActivity.this,showData_in_textView_class.class);         
intObj.putExtra("data", new Gson().toJson(list_of_reg_info);
startActivity(intObj);

and for getting data back

String json=getIntent.getStringExtra("data");
List<HashMap<String, String>> yourData=new Gson().fromJson("", new TypeToken<List<HashMap<String, String>>>() {
                        }.getType());

here you can learn more about Gson, trust me, is very powerfull !!

Apperside
  • 3,542
  • 2
  • 38
  • 65
0

I think you can directly call

reg_info_lists=db.get_all_info();

in you other activity instead of passing the values. That is a workaround that I can think of immediately.

Did you add any Logs to check the data to find out what is in intename.getSerializableExtra("data");?

Naresh
  • 3,174
  • 1
  • 17
  • 22