HashMap <string,string =“”>文本到第二个活动中的TextView

时间:2017-12-13 21:27:45

标签: java android android-intent hashmap textview

我有ListView使用HashMap<String, String>我希望传递给第二个活动。

关于第二个动作,我有一些TextView,我需要一个TextView来设置HashMap上所选项目的文字。

问题是代替键或值,(“Example1”或“Example2”)我在TextView中得到了这个:

  

{First = Example1,Second = Example2}

主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView resultsListView = (ListView) findViewById(R.id.listView);
    EditText buscar = (EditText) findViewById(R.id.busqueda);
    final TreeMap<String, String> nameAddresses = new TreeMap<>();

    nameAddresses.put   ("Example1", "Example2");



    final List<HashMap<String, String>> listItems = new ArrayList<>();
    final SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
            new String[]{"First", "Second"},
            new int[]{R.id.text1, R.id.text2});

    Iterator it = nameAddresses.entrySet().iterator();
    while (it.hasNext())
    {
        HashMap<String, String> resultsMap = new HashMap<>();
        Map.Entry pair = (Map.Entry)it.next();
        resultsMap.put("First", pair.getKey().toString());
        resultsMap.put("Second", pair.getValue().toString());
        listItems.add(resultsMap);
    }


    resultsListView.setAdapter(adapter);
    resultsListView.setFastScrollEnabled(true);


    resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           Intent intent = new Intent (MainActivity.this, SecondActivity.class);
           intent.putExtra("Titulo", resultsListView.getItemAtPosition(position).toString());

            startActivity(intent);
        }
    });

第二项活动:

TextView mTextView;
TextView mTextView2;
TextView mTextView3;
TextView mTextView4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    mTextView = (TextView) findViewById(R.id.textView2);
    mTextView2 = (TextView) findViewById(R.id.textView3);
    mTextView3 = (TextView) findViewById(R.id.tTitulo);
    mTextView4 = (TextView) findViewById(R.id.tSubtitulo);
    button = (ImageButton) findViewById(R.id.imageButton);


    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        mTextView3.setText(bundle.getString("Titulo"));

1 个答案:

答案 0 :(得分:0)

在将HashMap传递给第二个活动并在那里重建数据之前,应该使用JSON序列化HashMap。 Android支持此功能,因此您可以使用org.json.JSONObject。这意味着您可以:

Intent intent = new Intent (MainActivity.this, SecondActivity.class);
intent.putExtra("Titulo", new JSONObject(resultsListView.getItemAtPosition(position)).toString());

startActivity(intent);

在你的SecondActivity做:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    JSONObject myHashMap = JSONObject(bundle.getString("Titulo"));
    mTextView3.setText(myHashMap.optString('First', 'error'));
}

myHashMap 不是 HashMap,但您可以以类似的方式使用它。您详细了解了here