![](/img/trans.png)
[英]How to pass selected listview item to another listview in the next activity
[英]Pass listview selected text to another activity
抱歉,無法正常工作,在onclick事件期間發生錯誤。 我想將選定的文本傳遞回啟動活動。 我是新來的,所以請放輕松:)
public class selectTee extends ListActivity{
String[] tees_list;
String value = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tees_list = getResources().getStringArray(R.array.tees_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, tees_list));
final ListView teelist = getListView();
teelist.setChoiceMode(1);
teelist.setTextFilterEnabled(false);
teelist.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {
//Error occurs during the onclick event
Intent data = new Intent();
data.putExtra(value, selectedInt);
setResult(RESULT_OK, data);
finish();
}
});
}
}
好了,讓它正常工作,我更改了以下代碼:
public void onActivityResult(int requestCode,int resultCode,Intent data){
if (requestCode == request_Code) {
if (resultCode == RESULT_OK){
Button revisedButton = (Button) findViewById(R.id.button1);
String btext = data.getData().toString();
revisedButton.setText((CharSequence) btext);
}
}
}
和回國活動
public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {
String selection =((TextView) arg1).getText().toString();
System.out.println(selection);
Intent data = new Intent();
Uri uri = Uri.parse(selection);
System.out.println(uri);
data.setData(uri);
setResult(RESULT_OK, data);
finish();
}
});
采用
Intent intent = new Intent(getBaseContext(), YOUCLASS.class);
intent.putExtra("TEXT", selectedInt);
//startActivity(intent) if you want to start an activity when its clicked
看起來您從未設置過“值”,因此它仍然為null。 我認為這可能是您的問題。 您可以在調用putExtra之前嘗試設置該String值嗎?
另外,要從單擊的視圖中獲取選定的文本。 請執行下列操作:
String textOfSelectedItem =((TextView) arg1).getText().toString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.