在第二个活动中将TreeMap值传递给工具栏标题

时间:2017-11-23 22:17:51

标签: android listview treemap

我有一个带有ListView的TreeMap,我希望第二个活动获取ListView的选定项目并将其用作第二个活动的标题。 (使用字符串或数组而不是TreeMap,我可以使它工作,但TextWatcher将无法工作,使用TreeMap,TextWatcher工作正常)。

public class MainActivity extends AppCompatActivity  {


@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);
    TreeMap<String, String> nameAddresses = new TreeMap<>();

    nameAddresses.put   (   "Coso",                     "Desear       " );
    nameAddresses.put   (   "бажа́ти",                  "Desear       " );
    nameAddresses.put   (   "бала́кати",                "Charlar          " );


    final List<HashMap<String, String>> listItems = new ArrayList<>();
    final SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
            new String[]{"First Line", "Second Line"},
            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 Line", pair.getKey().toString());
        resultsMap.put("Second Line", 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("Want to Pass", resultsListView.getItemAtPosition(position).toString());


            startActivity(intent);
        }
    });
}

这是主要的活动,现在是第二个:

public class SecondActivity extends AppCompatActivity {

Toolbar mToolbar;
TextView mTextView;
TextView mTextView2;
ImageButton button;


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

    //changing statusbar color
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
    }


    mToolbar = (Toolbar) findViewById(R.id.toolbar2);
    mTextView = (TextView) findViewById(R.id.textView2);
    mTextView2 = (TextView) findViewById(R.id.textView3);
    button = (ImageButton) findViewById (R.id.imageButton);




    Bundle bundle = getIntent().getExtras();      


    if (bundle != null) {                         

        mToolbar.setTitle(bundle.getString("Want to Pass"));   





        if (mToolbar.getTitle().toString().equalsIgnoreCase("Coso")) {           писа́ти
            mToolbar.setSubtitle("Escribir");  

ListView在每个项目中显示两个元素(例如“Coso和Desear”),我希望这两个元素中的任何一个在第二个活动的toolBar中用作标题... .....

0 个答案:

没有答案