单击一行时,将文本从Listview设置为Another Activity的EditText?

时间:2013-03-25 08:59:49

标签: android-listview

我有一个listview,我想将listview的文本传递给另一个Activity的edittext。可以帮忙吗?

    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);
}

当我点击第一行时,下一个活动将在edittext中显示“Cloudy”。

1 个答案:

答案 0 :(得分:1)

你可以为你的列表视图创建一个listview onItemClickListener,当用户点击列表项/行时,你可以从该行获取文本,你可以将它传递给你要调用的下一个Activity,你通过bundle和Intent传递数据如下所示

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);
    }
});

在下一个活动中,您可以获得您通过此类

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