gpt4 book ai didi

android - 如何使 ListView (已创建)可点击(Android)?

转载 作者:搜寻专家 更新时间:2023-10-30 20:39:43 25 4
gpt4 key购买 nike

我制作了一个列表,现在我希望当用户单击一个项目时,它会打开一个显示 PKMN 数据的新屏幕。这是我的代码:

关东类

public class Kanto extends ActionBarActivity {

//fasendu listaa = making list
ListView listView; //criandu var = making variable
String[] pokemonsKanto = {
"#1 Bulbasaur", "#2 Ivysaur", "#3 Venusaur"
}; //lista = list

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

//continuandu a lista = the other part to the list work
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> array = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pokemonsKanto);
listView.setAdapter(array);
//lista cabada = finished
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kanto, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}

activity_kanto.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Kanto">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>

我该怎么做?抱歉,我是个新手。

最佳答案

现在您只需为您的 ListView 设置一个 onItemClickListener,如下所示:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//create an Intent to your new `Activity` with PKMN data
Intent pkmnActivityIntent = new Intent(Kanto.this, YourPKMNActivity.class);

//pass your pkmn number and name (from your `String` array) in the `Intent`, so it can be shown in the new `Activity`
pkmnActivityIntent.putExtra("name",pokemonsKanto[position] );
//start your new Activity
startActivity(pkmnActivityIntent );
}
});

当用户单击列表中的项目时,将激活此监听器。您可以在 listview.setAdapter() 方法之后立即设置它。

编辑:不要忘记在 manifest.xml 文件中声明新的 Activity。然后在您的新 Activity 中,使用以下命令获取 pkmn 名称:

String pkmnName = getIntent().getStringExtra("name");

现在您可以在 TextView 或其他内容中显示您的 pkmnName。

关于android - 如何使 ListView (已创建)可点击(Android)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24856115/

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