How to display the values selected in a listview to another activity, listviewactivity

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.