2017-10-17 4 views
0

J'ai LinearLayout avec un listView (liste des musiques) et un autre LinearLayout avec deux textView pour le titre et l'artiste des fichiers de musique, la liste fonctionne correctement. Ma question est comment dois-je définir la méthode songPicked() (android:onClick="songPicked") pour obtenir la musique sélectionnée pour la lecture.Comment obtenir l'élément sélectionné de la listeVoir

Merci.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="#333838" 
tools:context=".MainActivity" 
> 

<ListView 
    android:id="@+id/SongListView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="5dp" 
android:id="@+id/colLayout" 
android:clickable="true" 
android:onClick="songPicked"> 

<TextView 
    android:id="@+id/song_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/fileTitle" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/song_artist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/fileDec" 
    android:textSize="18sp" /> 

public class MainActivity extends AppCompatActivity { 

    private ArrayList<SoundInfo> SoundArray; 
    private ListView SongListView; 
    TextView totalSongs; 
    String TAG = "path"; 
    int fileCounter; 
    Uri filePathUri; 
    public Cursor musicCursor; 
    LinearLayout colLayout; 


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


     SongListView = (ListView) findViewById(R.id.SongListView); 
     totalSongs=(TextView)findViewById(R.id.totalSongs); 

     SoundArray = new ArrayList<SoundInfo>(); 
     colLayout=(LinearLayout)findViewById(R.id.colLayout); 

     SoundAdapter songAdt = new SoundAdapter(this, SoundArray); 
     SongListView.setAdapter(songAdt); 

     RetrieveSoundInfo(); 
    } 


    public void songPicked(View view){ 

     Toast.makeText(getApplicationContext(), "play", Toast.LENGTH_LONG).show(); 

    } 
} 

Répondre

1

Essayez d'utiliser

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

     songPicked(position); 

    } 

}); 

et le changement:

public void songPicked(int position){ 

    Toast.makeText(getApplicationContext(), "clicked on "+position, Toast.LENGTH_LONG).show(); 

} 
+0

cela fonctionne parfaitement, merci. –