How to display the values selected in a listview to another activity, listviewactivity
I am creating a simple app. One function is to get the selected value from the first listView and display it to the second activity. Intent is used in the middle for transmission, but now there is a problem, I don't know how to be in the second activity.
MainActivity. java
package com.devleb.listviewdemo; import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView; public class MainActivity extends ListActivity { TextView txt; private static final String[] items = { "doctor", "engineer", "lawer", "developer", "employee", "business man", "auditer", "cashier" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); txt = (TextView) findViewById(R.id.txt); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); // txt.setText(items[position]); Intent i = new Intent(this, SecondActivity.class); i.putExtra("testonArray", items); startActivity(i); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }SecondActivity.javapackage com.devleb.listviewdemo; import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.EditText;import android.widget.TextView; public class SecondActivity extends Activity { TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Bundle extras=getIntent().getExtras(); String [] values = extras.getStringArray ("testonArray"); txt = (TextView) findViewById (R.id.txt2); if (values != null && values.length > 0 && txt != null){ txt.setText(values [0]); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.second, menu); return true; } }
Solution
First Activity code:
+@Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); // txt.setText(items[position]); // Try to send the items[position] in the intent Intent i = new Intent(this, SecondActivity.class); i.putExtra("testonArray", items[position].toString()); startActivity(i); }
Second activity Code
Bundle extras=getIntent().getExtras(); String selected_item=extras.getString("testonArray"); txt = (TextView) findViewById (R.id.txt2); txt.setText(selected_item);
Address: http://www.itmmd.com/201411/215.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.