2013-04-04 141 views
2

我一直在努力使用一些教程来将一个listview选项传递给一个新的活动,并使其成为标题(我将在稍后使用它来完成其他任务)。我已成立了由什么是最好的一个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); 
+0

格兰所需的值你能张贴你所得到的logcat的? – jcw 2013-04-04 13:23:38

回答

2

对于所有你需要得到被选为该项目:

final String selected = items[position]; 

或者作为doctoror驱动器已建议

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 
+1

或更好:final String selected =(String)parent.getSelectedItem(); – 2013-04-04 12:04:31

+0

@DoctororDrive - 我不知道你可以这样做,谢谢 – jcw 2013-04-04 12:06:16

+0

非常感谢,但它给我一个错误,由于代理Doctoror Drive的'父母'给了...我知道这是示例代码,但是什么'parent'代表 – Ryaller 2013-04-04 12:21:25

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 Activitiy获得价值:

String SelectedItem = getIntent().getStringExtra("selectedItem"); 
0

公共无效onItemClick(适配器视图父,观景,INT位置,长ID)

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

}

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); 

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

//Activity_2 

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

现在你已经在getdata_from_list

相关问题