depaulagu
depaulagu

Reputation: 169

How to make a listview(already created) clickable (Android)?

I made a list and now I want when the user clicks an item, it opens a new screen showing the PKMN data. Here's my code:

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>

How can I do that? Sorry, I'm a completely newbie .

Upvotes: 2

Views: 199

Answers (2)

joao2fast4u
joao2fast4u

Reputation: 6892

Now you just have to set an onItemClickListener to your ListView, like this:

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

This listener is activated when the user clicks an item in your list. You can set it right after listview.setAdapter() method.

Edit: Do not forget to declare your new Activity on your manifest.xml file. Then in your new Activity, just get the pkmn name by using:

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

Now you can show your pkmnName in a TextView or something.

Upvotes: 1

Delgado
Delgado

Reputation: 226

You need to add a listener to the items of the list,for example I will give you how to show the text of the item in an alert dialog (you can put the title of the item in a string to pass it in intent rather than display it in an alert dialog) :

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

Upvotes: 0

Related Questions