[英]How to transfer the clicked item in listview to editText in another activity in android
I want to get the item on list view to edit Text in another activity. 我想在列表视图中获取该项目,以在另一个活动中编辑文本。 When clicked on list view item, I want to transfer the item in another activity in edit Text. 当单击列表视图项目时,我要在编辑文本的另一个活动中转移该项目。
You have to make onItemClickListner of listview like that. 您必须像这样使listview的onItemClickListner。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values: 然后在新的活动中,检索以下值:
Bundle extras = getIntent().getExtras();
if (extras != null) { String value = extras.getString("new_variable_name"); }
And finally set Value to editText like this 最后像这样将Value设置为editText
editText.setText(value);
Hope this will help you. 希望这会帮助你。
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences . 您可以使用SharedPreferences 。 And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text")
. 并且,当您将ListView的内容传递给下一个活动时,可以使用editText.setText("Your Text")
。
You can also pass your data through intents from which you are calling your new activity. 您还可以通过调用新活动的意图传递数据。
create onClick
method like this. 创建这样的onClick
方法。
ListView list = (ListView) findViewById(R.id.newsList); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) { NewsItem item = (NewsItem) adapter.getItem(position); Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class); intent.putExtra(KEY, item.getHeadline()); startActivity(intent); } });
In next activity 在下一个活动中
Intent intent = getIntent(); headline = intent.getStringExtra(KEY);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.