2014-07-21 1 views
2

J'ai fait une liste et maintenant je veux quand l'utilisateur clique sur un élément, il ouvre un nouvel écran montrant les données PKMN. Voici mon code:Comment faire un clickview (déjà créé) cliquable (Android)?

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> 

Comment puis-je faire cela? Désolé, je suis complètement novice.

Répondre

1

Maintenant, il vous suffit de définir un onItemClickListener à votre ListView, comme ceci:

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

Cet écouteur est activé lorsque l'utilisateur clique sur un élément dans votre liste. Vous pouvez le définir juste après la méthode listview.setAdapter(). Edit: N'oubliez pas de déclarer votre nouveau Activity sur votre fichier manifest.xml. Puis dans votre nouvelle Activity, juste obtenir le nom de PKMN en utilisant:

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

Maintenant, vous pouvez montrer votre pkmnName dans un TextView ou quelque chose.

+0

Super, maintenant, comment je fais des objets différents, ouvrir des choses différentes. – gustavodp

+0

Maintenant, comment je fais différents objets ouvre des choses différentes? – gustavodp

+0

Cela dépend, mais vous pouvez utiliser la valeur 'int position' pour démarrer votre nouvelle activité en fonction de cette valeur. – joao2fast4u

0

Vous devez ajouter un écouteur aux éléments de la liste, par exemple je vais vous montrer comment afficher le texte de l'élément dans une boîte de dialogue d'alerte (vous pouvez placer le titre de l'élément dans une chaîne pour le passer dans l'intention plutôt que de l'afficher dans une boîte de dialogue d'alerte):

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