Hema
Hema

Reputation: 39

Setting text from Listview to EditText of Another Activity when a row is clicked?

I have a listview, I want to pass the Text of the listview to edittext of the another Activity.Can u help?

    public class MainActivity extends Activity {

    private ListView listView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Weather weather_data[] = new Weather[]
        {
            new Weather(R.drawable.weather_cloudy, "Cloudy"),
            new Weather(R.drawable.weather_showers, "Showers"),
            new Weather(R.drawable.weather_snow, "Snow"),
            new Weather(R.drawable.weather_storm, "Storm"),
            new Weather(R.drawable.weather_sunny, "Sunny")
        };

        WeatherAdapter adapter = new WeatherAdapter(this, 
      R.layout.listview_item_row, weather_data);


        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);
}

When I click the first row,the next activity will show "Cloudy" as in the edittext.

Upvotes: 0

Views: 1071

Answers (1)

nidhi_adiga
nidhi_adiga

Reputation: 1124

You can create a listview onItemClickListener for ur listview and when user clicks on list item/row u can get the text from that row and u can pass it to next Activity that u r going to call and u r passing data via bundle and Intent like below

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
        String text= arg0.getItemAtPosition(position)

        Bundle bundle = new Bundle();

        bundle.putString("URTEXT", text);

        Intent intent = new Intent(MainActivity.this,
                        NextActivity.class);

        intent.putExtras(bundle);
        startActivity(intent);
    }
});

In Next Activity U can get that data that u passed through bundle like this

Intent intent = getIntent();
String tEXT = intent.getIntExtra("URTEXT", 0);
EditText et= (EditText)findViewById(editTextID);
et.setText(tEXT, TextView.BufferType EDITABLE);

Upvotes: 1

Related Questions