我正在尝试创建一个显示列表视图的应用程序,当您单击该列表中的特定项目时,它将显示包含特定信息的Toast。
到目前为止,我从this site获得了此代码:
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG);
}
那么我如何修改代码,例如,当单击列表中的“android”时显示包含Android信息的吐司?当然,当点击“iPhone”等时,将显示包含不同信息的不同吐司。
感谢您提供的任何帮助:)
答案 0 :(得分:3)
使String[] values
成为一个类变量。
缺少toast.show()。
Toast.makeText(this, values[position] + " selected", Toast.LENGTH_LONG).show();
<强>更新强>
public class MyListActivity extends ListActivity
{
private String[] values ;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, values[position]+ " selected", Toast.LENGTH_LONG);
}
答案 1 :(得分:2)
试试这个:
public class MyListActivity extends ListActivity {
private String[] _values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
private String[] _details = new String[] { "Android Details", "iPhone Details",
"WindowsMobile Details", "Blackberry Details", "WebOS Details", "Ubuntu
Details", "Windows7 Details", "Max OS X Details", "Linux Details", "OS/2 Details" };
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, _values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, _details[position], Toast.LENGTH_LONG).show();
}
希望有所帮助。快乐的编码!