0

我有一个名为的类register_information,看起来像

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

我使用了一些代码来获取MainActivity像这样注册的所有名称和密码。

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);
                   }

现在我想将这些所有名称和密码传递到另一个窗口,以便按意图将它们显示为列表视图。我用这样的意图

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

在我showData_in_textView_class的身上,我收到了这样的意图

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

} 

}

但它没有用。我怎样才能做到这一点??

4

3 回答 3

1

您可以使用 Intent 传递可序列化的变量。而不是不可序列化的List,只需使用可序列化的ArrayList

于 2013-07-19T09:36:38.890 回答
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 !!

于 2013-07-19T10:28:09.720 回答
0

我想你可以直接打电话

reg_info_lists=db.get_all_info();

在您的其他活动中,而不是传递值。这是我可以立即想到的解决方法。

您是否添加了任何日志来检查数据以找出其中的内容intename.getSerializableExtra("data");

于 2013-07-19T11:00:39.213 回答