通过自定义列表视图,从活性的去活动B [英] Go from activity A to activity B via custom listview
问题描述
我已经成功地填充一个Activity(我们称之为活性A 的),带有自定义列表视图,工作正常。
I've managed to populate an Activity (which we'll call Activity A) with a custom listview which works fine.
当从在活动A的项目列表中的项目的用户点击,我想这个项目的细节传递到活动B.
When the user clicks on an item from the list of items in Activity A, I want to pass details of that item into Activity B.
这是我的一个活动是这样的:
This is what my activity A looks like:
public class ChannelList extends Activity implements OnItemClickListener {
public static final String[] names = new String[] { "BBC One", "BBC Two" };
public static final String[] descs = new String[] { "London, SD", "England, SD"};
ListView listView;
List<ChannelItem> channelItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel_list);
channelItems = new ArrayList<ChannelItem>();
for (int i = 0; i < names.length; i++) {
ChannelItem item = new ChannelItem(names[i], descs[i]);
channelItems.add(item);
}
listView = (ListView) findViewById(R.id.listView1);
ChannelListViewAdapter adapter = new ChannelListViewAdapter(this, R.layout.item_channel_list, channelItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// Toast toast = Toast.makeText(getApplicationContext(), "Item " + (position + 1) + ": " + channelItems.get(position), Toast.LENGTH_SHORT);
// toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
// toast.show();
Intent intent = new Intent(this, ScheduleList.class);
startActivity(intent);
}
}
基于Android API文档我间pretation
,我应该数据传递到使用的活动B调用Intent.putExtra();
Based on my interpretation of the Android API documentation, I'm supposed to pass data into the Activity B call using Intent.putExtra();
的问题是,如何通过列表项的名称和描述元素的用户点击?
例如,当我打电话的活动B,它应该能够如果BBC一个是从列表视图点击下载电视节目表数据为BBC一。如何使活动B知道哪些列表项的点击触发活动B?
推荐答案
如何
intent.putExtra("name", names[position]);
在活动
A的 onItemClick
方法以及
String name = getIntent().getStringExtra("name");
在活动
B的的onCreate
方法?
这篇关于通过自定义列表视图,从活性的去活动B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!