【问题标题】:how to get Hash table Arraylist to other intent?如何将哈希表 Arraylist 用于其他意图?
【发布时间】:2012-02-02 06:51:13
【问题描述】:

我在数组列表中有 hashtbles。

 List<Hashtable<String, String>> info = new ArrayList<Hashtable<String, String>>();


 Hashtable<String, String> hm = new Hashtable<String, String>();
// Put elements to the map

hm.put("Read_Flag", s1);
hm.put("sms_received_id", s2);
hm.put("Sender_Id", s3);
hm.put("Sender_Name", s4);
hm.put("Patient_Name", s5);
hm.put("Received_Date", s6);
hm.put("Received_Text", s7);
hm.put("Received_Text_Full", s8);
hm.put("AttachmentFlag", s9);

// Get a set of the entries
Set<?> set = hm.entrySet();
// Get an iterator
Iterator<?> it = set.iterator();
// Display elements
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
 // System.out.print(me.getKey() + ": ");
 // System.out.println(me.getValue());
}
//System.out.println(hm);

info.add(hm);

这里的信息包含我的哈希表。我怎样才能把这个“信息”对象放到其他类/意图中?

谢谢...

【问题讨论】:

  • 可能只有我,但我不明白你的要求。也许一个例子或更多解释会有所帮助。
  • 不确定您的意思?请你把它简单一点。
  • 如何在其他类中获取此“信息”对象?
  • @HaPPy 使用 Serializable 或 Parcelable。
  • @LalitPoptani 请给我一个示例代码

标签: java android arraylist hashtable


【解决方案1】:

List&lt;Hashtable&lt;String, String&gt;&gt; list 创建一个扩展Serializablegetter setter 的类

Custom.java

public class Custom implements Serializable{

    private static final long serialVersionUID = 4466821913603037341L;
    private List<Hashtable<String, String>> list;


    public List<Hashtable<String, String>> getList() {
        return list;
    }

    public void setList(List<Hashtable<String, String>> list) {
        this.list = list;
    }
}

传递到下一个 Activity。

List<Hashtable<String, String>> list = new ArrayList<Hashtable<String,String>>();

Custom custom = new Custom();
custom.setList(list);
intent.putExtra("myobj", custom);

在下一个活动中检索

Intent intent = getIntent();
Custom custom = (Custom) intent.getSerializableExtra("myobj");
List<Hashtable<String, String>> list = custom.getList();

【讨论】:

    【解决方案2】:
    List<Hashtable<String, String>> info = new ArrayList<Hashtable<String, String>>(); 
    String[] selItemArray = new String[info .size()];
                //copy your List of Hashtable Strings into the Array ,and then pass it in your intent
                // ....
                Intent intent = new Intent(InfoClass.this, AnotherClass.class);
                intent.putExtra("info_array", selItemArray);
                startActivityForResult(intent, 0);  
    

    数组列表的类型

    putIntegerArrayListExtra(String name, ArrayList<Integer> value)
    
            putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
    
            putStringArrayListExtra(String name, ArrayList<String> value)
    
            putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)
    
        Then you can read from you next activity by replacing put with get with key string as argument,eg
    
    myIntent.getStringArrayListExtra("key");
    

    数组:

    Bundle b=new Bundle();
    b.putStringArray(key, new String[]{value1, value2});
    Intent i=new Intent(context, Class);
    i.putExtras(b);
    
    
    
    In order to read:
    
    Bundle b=this.getIntent().getExtras();
    String[] array=b.getStringArray(key);
    

    希望这会对你有所帮助。

    参考:Passing a list of objects between Activities

    【讨论】:

    • 我怎样才能在其他班级得到这个?
    • 听起来不错,但我不想要数组,我想要有哈希表的 Arraylist。该怎么做?
    • 你需要实现 Parcelable 接口并重写一些函数来序列化/反序列化对象
    【解决方案3】:

    将您的 Hashtable 转换为 Bundle,并使用 intent.putExtra() 传递 Bundle 这是例子

    Intent i = new Intent(this,yourclass.class);

    i.putExtra("info",info);

    你可以得到其他意图中的值

    捆绑额外=getIntent().getExtras();

    extra.geStrinf("信息");

    【讨论】:

      【解决方案4】:

      将您的 Hashtable 转换为 Bundle,并使用 intent.putExtra() 传递 Bundle

      【讨论】:

        猜你喜欢
        • 2017-11-24
        • 2022-11-03
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多