gpt4 book ai didi

java - ListView 中的可点击项目

转载 作者:行者123 更新时间:2023-12-01 18:52:50 25 4
gpt4 key购买 nike

我正在尝试使我的 ListView 项目可点击;但无法让它正常工作。当我单击某个项目时,它会切换屏幕,但不会切换到所需的屏幕。这是代码

private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
lv = (ListView) findViewById(R.id.listView1);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> menu_Items = new ArrayList<String>();
menu_Items.add("Fill Treatment Form");
menu_Items.add("View Medical History");
menu_Items.add("View Medication");
menu_Items.add("View Diet");
menu_Items.add("View First Aid");
menu_Items.add("Look Up Map");
menu_Items.add("Account Settings");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menu_Items);
lv.setAdapter(arrayAdapter);


lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(), FillTreatmentActivity.class);
startActivity(i);

}

});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg1, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(), MedicalHistoryActivity.class);
startActivity(i);
}
});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg2, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(), MedicationActivity.class);
startActivity(i);
}
});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg3, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(),DietActivity.class);
startActivity(i);
}
});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg4, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(),FirstAidActivity.class);
startActivity(i);
}
});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg5, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(),MapActivity.class);
startActivity(i);
}
});

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg6, View view, int position, long id) {

Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
startActivity(i);
}
});
}

当我单击任何 ListView 项目时,帐户设置 Activity 将打开。我是新手,所以请不要评判我的编码:)

最佳答案

ListView 只能有一个 OnItemClickListener,因此除了最后一个之外的所有内容都会被丢弃。这就是为什么无论您点击哪个项目,SettingsActivity 都是开放的。

要解决这个问题,请使用一个监听器并让该监听器处理不同的选项。

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg1, View view, int position, long id) {
switch(position) {
case 0:
// start activity 1
break;
case 1:
// start activity 2
break;
case 2:
// start activity 3
break;
// more case statements

}
});

关于java - ListView 中的可点击项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15205084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com