我有一个列表视图,其中包含每个单元格内的项目。我能够获得Toast警报,但我希望它能够开始一个新活动,其中单元格的内容显示为textview。这甚至可能吗?这是我的参考代码:
公共类Activity扩展ListActivity实现OnItemClickListener {
ArrayList<HashMap<String,String>> gameCollection = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=getListView();
listView.setOnItemClickListener(this);
//Populate view with data
gameCollection.add(displayVideoGame("PlayStation","FIFA 14"));
gameCollection.add(displayVideoGame("PlayStation","Thief"));
gameCollection.add(displayVideoGame("PlayStation","Watch Dogs"));
gameCollection.add(displayVideoGame("PlayStation","Battlefield 4"));
gameCollection.add(displayVideoGame("PlayStation","Second Sun"));
gameCollection.add(displayVideoGame("PlayStation","Mario Bros"));
//Calling the Simple Adapter calling gameCollection List and setting the predefined layout
SimpleAdapter adapter = new SimpleAdapter(this,this.gameCollection, android.R.layout.simple_list_item_1, new String[] {"PlayStation"},new int[] {android.R.id.text1});
//set adapter to collection
System.out.println(gameCollection);
setListAdapter(adapter);
}
//Create Private Method - Returns HashMap with key-value pairs
private HashMap<String, String> displayVideoGame(String key, String value)
{
HashMap<String, String> videoGameHashMap = new HashMap<String, String>();
videoGameHashMap.put(key, value);
return videoGameHashMap;
}
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3)
{
String item=adapter.getItemAtPosition(position).toString();
Toast.makeText(Activity.this, "You Click on:"+item, Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
在onItemClick
的正文中,您可以添加逻辑以启动新的活动:
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putStringExtra(MyOtherActivity.TEXT_TO_DISPLAY, item);
startActivity(intent);
第一行创建一个新的意图,将启动&#34; MyOtherActivity&#34;活动(显然你想要插入自己的活动名称)。
第二行将您要显示的项目文本粘贴到意图包中,以便您的新活动可以在以后使用它。
第三行开始你意图中描述的活动。
然后,在&#34; MyOtherAcitivy.onCreate()&#34;:
String text = getIntent().getExtras().getString(TEXT_TO_DISPLAY);
TextView tv = (TextView) findViewById(R.id.id_of_textview);
tv.setText(text);
此处,第一行从包中取出您的字符串,第二行找到您要填充的TextView,第三行将TextView的文本设置为您从第一个活动发送的字符串。
您还需要在MyOtherActivity中添加TEXT_TO_DISPLAY作为静态最终字符串。这是允许您在Extras包中找到字符串的关键。
public static final string TEXT_TO_DISPLAY = "PickSomeTextToUseThatIsClear";