2016-09-26 63 views
0

我在这里有一个代码,我想通过单击保存按钮来传递哈希映射函数。我看了Here,但找不到解决方案。如何从意图传递HashMap <String,List <String>>

上点击按钮,我需要通过hasmap.Anyone请帮助this.My代码是:

btnsave = (Button) findViewById(R.id.btn_save); 
    btnsave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      try { 
       expandableDataPump.getData(); 
       Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
//HERE I NEED TO PASS HASHMAP 
       intent.putStringArrayListExtra("details",(HashMap<String,>) expandableDataPump.getData()); 

       startActivity(intent); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 

       Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
      } 


     } 
    }); 

} 

public class ExpandableDataPump { 
    public HashMap<String, List<String>> getData() { 
     HashMap<String, List<String>> expandableListDetail = new HashMap<>(); 

     for (int i = 2; i < container.getChildCount(); i++) { 
      if (container.getChildAt(i) instanceof RelativeLayout) { 
       List<String> childs = new ArrayList<>(); 
       childs.add(((TextView)container.getChildAt(i).findViewById(R.id.textout)).getText().toString()); 
       expandableListDetail.put(txtHeading.getText().toString(), childs); 
      } 
     } 


     return expandableListDetail; 
    } 
} 

}

+0

你有什么问题?你得到任何异常/崩溃? –

+0

虽然编写代码的意图红线出现,我不能运行它.. –

回答

2

你做错了,一切都Collections序列化,为了通过HashMap的进入意图使用

putExtra(String key, Serializable obj) 

,并从意图获取数组回来,你做

getIntent().getSerializableExtra(String key) 

你的答案

btnsave = (Button) findViewById(R.id.btn_save); 
btnsave.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     try { 
      Intent intent = new Intent(getApplicationContext(), S.class); 
      intent.putExtra("details", getData()); 

      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
     } 


    } 
}); 

public HashMap< String, List<String> > getData() { 
    HashMap< String, List<String> > expandableListDetail = new HashMap<>(); 

    for (int i = 2 ; i < container.getChildCount() ; i++) { 
     if (container.getChildAt(i) instanceof RelativeLayout) { 
      List<String> childs = new ArrayList<>(); 
      childs.add(((TextView) container.getChildAt(i).findViewById(R.id.textout)).getText().toString()); 
      expandableListDetail.put(txtHeading.getText().toString(), childs); 
     } 
    } 
    return expandableListDetail; 
} 
+0

你可以编辑上面的代码 –

+0

以及它如何与哈希映射..我很困惑.. –

+0

intent.putStringArrayListExtra(“details”,( HashMap )expandableDataPump.getData()); 我在这里做错了吗? –

0

使用

extras.putSerializable("HashMap",Hash_Map); 
intent.putExtras(extras); 

而不是

intent.putStringArrayListExtra( “详细信息”,(HashMap的)expandableDataPump.getData());

btnsave = (Button) findViewById(R.id.btn_save); 
btnsave.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     try { 
      expandableDataPump.getData(); 
      Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
      intent.putExtra("details",(HashMap<String,>) expandableDataPump.getData()); 

      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 

      Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
     } 


    } 
}); 

}

+0

演员和Hash_map显示错误? –

+0

使用我提到的下面的代码..不需要添加额外的。 Intent intent = new Intent(getApplicationContext(),ExpandableList.class); intent.putExtra(“details”,(HashMap )expandableDataPump.getData()); startActivity(intent); –

+0

intent.putExtra(“details”,(HashMap )expandableDataPump.getData());这句话是不完整的 –

2

您可以通过以下只是下面的指令做到这一点。

首先使用实现Serializable创建类,如下所示。

public class DataWrapper implements Serializable{ 

    private HashMap<String,List<String>> hasmap; 

    public DataWrapper(HashMap<String,List<String>> hasmap){ 
     this.hasmap= hasmap; 
    } 

    public HashMap<String,List<String>> getHashMap(){ 
     return this.hasmap; 
    } 
} 

从你想要通过的活动HashMap<String,List<String>>把下面的代码。

DataWrapper dw = new DataWrapper(expandableDataPump.getData()); 

Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
intent.putExtra("details", dw); 
startActivity(intent); 

在你ExpandableList活动取得DataWrapper对象,如下图所示,从该对象检索您HashMap

try{ 
    Intent intent = getIntent(); 
    DataWrapper dw = (DataWrapper) intent.getSerializableExtra("details"); 
    HashMap<String,List<String>> hasmap = dw.getHashMap(); 
} catch(Exception e){ 
} 

就是这样。它为我工作。

+0

如果代码与我的上述代码有关 –

+0

@jitesh为什么要在hashmap上创建另一个包装呢?直到和除非用户有一个自定义类 –

+0

@lephaphase我更改代码请参阅根据您的代码。 –

0

要通过HashMap

btnsave.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     try { 
      Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
      intent.putExtra("details", expandableDataPump.getData()); 
      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 
}); 

ExpandableList检索,

HashMap<String,List<String>> hashmap = (HashMap<String,List<String>>) getIntent().getSerializableExtra("details"); 
相关问题