如何使listview(已创建)可点击(Android)?

时间:2014-07-21 00:06:40

标签: android xml database list android-listview

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

Kanto.class

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>

我该怎么做?对不起,我是一个全新的人。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

  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();
    }
});