【问题标题】:How to make a listview(already created) clickable (Android)?如何使列表视图(已创建)可点击(Android)?
【发布时间】:2025-12-25 03:30:10
【问题描述】:

我创建了一个列表,现在我希望当用户单击一个项目时,它会打开一个显示 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>

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

【问题讨论】:

    标签: android xml database list android-listview


    【解决方案1】:

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

    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。

    【讨论】:

    • 太好了,现在,我如何制作不同的物品,打开不同的东西。
    • 现在,我如何制作不同的物品会打开不同的东西?
    • 视情况而定,但您可以使用 int position 值根据该值启动新 Activity。
    • 我希望当我单击一个 pkmn 名称时,它会打开一个新屏幕,当我单击另一个 pkmn 名称时,它会打开另一个屏幕
    • 我假装对每个 PKMN 数据使用不同的活动。已经接受,但我不能投票,需要 15 声望。
    【解决方案2】:

    你需要为列表中的项目添加一个监听器,例如我会告诉你如何在一个警告对话框中显示项目的文本(你可以将项目的标题放在一个字符串中传递它意图而不是在警报对话框中显示):

      listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
    
            //The title of the Item is recovered in a String
            String item = (String) listView.getAdapter().getItem(position);
    
            AlertDialog.Builder adb = new AlertDialog.Builder(kanto.this);
            //The title of the alert Dialog
            adb.setTitle("Your Item");
            //The name of the Item 
            adb.setMessage("You have selected : "+item);
            //the OK button
            adb.setPositiveButton("Ok", null);
            //show the alert dialog
            adb.show();
        }
    });
    

    【讨论】: