2

我一直在努力使用一些教程将列表视图选项传递给新活动并将其作为标题(稍后我将使用它做其他事情)。我已经设置了一个 OnClickListener 最好放在里面

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

String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

listView1.setAdapter(adapter);

listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 

    ?????

}
});

谢谢

编辑:额外代码

final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
4

4 回答 4

2

对于所有你将需要获得被选中的项目:

final String selected = items[position];

或者正如医生所建议的那样

final String selected = (String) parent.getSelectedItem();

然后,您需要将该字符串作为额外内容传递给您的新活动

Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);

然后最后在你的下一个活动中

    Intent in = getIntent();
    String name = in.getStringExtra(("name"));//gets name from intent
于 2013-04-04T12:01:59.653 回答
0

public void onItemClick(AdapterView<?> parent, View view,int position, long id)添加此代码。

Intent i = new Intent(getApplicationContext(), NextClass.class);
i.putExtra("selectedItem", items[position]);
startActivity (i);

在 NextClass Activity 中获得价值:

String SelectedItem = getIntent().getStringExtra("selectedItem");
于 2013-04-04T12:04:53.533 回答
0

public void onItemClick(AdapterView parent, View view,int position, long id)

String str = items[position];
Intent in = new Intent(getApplicationContext(), NextClass.class);
in.putExtra("itemkey", str);
startActivity (in);

}

于 2013-04-04T12:05:13.250 回答
0

将以下内容添加到您拥有的活动中listview

以下是variable将包含value您要传递给的other activity

在声明之前声明Oncreate

// Activity_1

public final static String send_to_other_activity="ListViewSelected_ID";

将以下代码添加到listView1.setOnItemClickListener

Intent i= new Intent(Recipe_List.this,Recipe_View.class);
i.putExtra(send_to_other_activity, string.valueof(position));
// itz (key-value) pair on the left key thru which u will access it on other place. on the right value that you want to pass
// Iam passing posiion to other activity here
startActivity(i);

现在在其他活动中通过将以下语句添加到其他活动的 oonCreate 中activity提取此值key

//Activity_2

getdata_from_list =getIntent().getStringExtra(Activity_1.send_to_other_activity);

现在你有了想要的价值getdata_from_list

于 2013-04-04T12:21:23.700 回答