我正在尝试将ListView项目从MainActivity传递到MenuActivity的TextView项目,该intent成功创建了第二个活动,但是当我尝试传递数据时,我遇到了问题。 MainActivity.Java是一个搜索屏幕,当用户选择将其带到菜单页面的餐馆时,它会在输入时显示餐馆名称列表。我希望他们的选择作为菜单标题转移到新活动。
MainActivity.Java
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);
String listItem = (String) lv.getItemAtPosition(position);
myIntent.putExtra("restaurant" ,listItem );
startActivity(myIntent);
}
}); // end of lv.setOnItemClickListener
MenuActivity.Java
public class MenuActivity extends ExpandableListActivity {
// textview / menu header
private TextView menuHeader;
// parent list items
private static final String arrGroupElements[] = {"Appetizers", "Main Course", "Desserts",
"Beverages"};
// strings for child elements
private static final String arrChildElements[][]={
{"Appetizer 1", "Appetizer 2", "Appetizer 3", "Appetizer 4", "Appetizer 5"},
{"Main Course 1", "Main Course 2", "Main Course 3", "Main Course 4", "Main Course 5"},
{"Dessert 1", "Dessert 2", "Dessert 3", "Dessert 4", "Dessert 5"},
{"Beverage 1", "Beverage 2", "Beverage 3", "Beverage 4", "Beverage 5"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_screen);
// added code
setListAdapter(new ExpandableListAdapter(this));
menuHeader = (TextView) findViewById(R.id.articleHeaderTextView);
Bundle extras = getIntent().getExtras();
String searchData = extras.getString("restaurant");
menuHeader.setText(searchData);
}
答案 0 :(得分:0)
试试这个
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);
String listItem = (String) lv.getItemAtPosition(position);
myIntent.putExtra("restaurant" ,listItem );
startActivity(myIntent);
setResult(RESULT_OK, intent);
}
});
并在菜单活动中覆盖onactivityresults方法
public class MenuActivity extends ExpandableListActivity {
// textview / menu header
private TextView menuHeader;
// parent list items
private static final String arrGroupElements[] = {"Appetizers", "Main Course", "Desserts",
"Beverages"};
// strings for child elements
private static final String arrChildElements[][]={
{"Appetizer 1", "Appetizer 2", "Appetizer 3", "Appetizer 4", "Appetizer 5"},
{"Main Course 1", "Main Course 2", "Main Course 3", "Main Course 4", "Main Course 5"},
{"Dessert 1", "Dessert 2", "Dessert 3", "Dessert 4", "Dessert 5"},
{"Beverage 1", "Beverage 2", "Beverage 3", "Beverage 4", "Beverage 5"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_screen);
// added code
setListAdapter(new ExpandableListAdapter(this));
menuHeader = (TextView) findViewById(R.id.articleHeaderTextView);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK)
{
String searchData=data.getExtras().getString("restaurant");
menuHeader.setText(searchData);
}