ImonBayazid
ImonBayazid

Reputation: 1096

passing a List of string by putExtra method using intent

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??

Upvotes: 0

Views: 1680

Answers (3)

Naresh
Naresh

Reputation: 3179

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");?

Upvotes: 0

Apperside
Apperside

Reputation: 3602

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 !!

Upvotes: 0

Sean
Sean

Reputation: 5256

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

Upvotes: 1

Related Questions