gpt4 book ai didi

java - Android ListView-为每个项目打开新 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:35:44 26 4
gpt4 key购买 nike

您好,我正在尝试让我的 ListView 针对每个部分打开不同的 Activity 。截至目前,我可以打开一个 Activity ,但他们都打开相同的 Activity 。我需要知道如何改变它,例如:

item1 会打开activity1item2 将打开 activity2 等等。

任何帮助将不胜感激,这是我的代码(一些评论来 self 遵循的教程,对于我的经验不足深表歉意):

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);

//get listview from xml
listView = (ListView)findViewById(R.id.listView);

//defined array to show in listview
String[] values = new String[]{"About the App",
"How to Use",
"Build log",
};
//define a new context
//First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data

ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);

//Assign adapter to listview
listView.setAdapter(adapter);

// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*//listview clicked item index
int itemPosition = position;

//Listview clicked item value
String itemValue = (String)listView.getItemAtPosition(position);

//show alert
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/

Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);

}
});
}

最佳答案

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*//listview clicked item index
int itemPosition = position;

//Listview clicked item value
String itemValue = (String)listView.getItemAtPosition(position);

//show alert
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
switch(itemPosition)

case 0 :Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);
break;
case 1 :Intent appInfo = new Intent(About.this, Activity1.class);
startActivity(appInfo);
break;
case 2 :Intent appInfo = new Intent(About.this, Activity2.class);
startActivity(appInfo);
break;
}
});

关于java - Android ListView-为每个项目打开新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466096/

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