amir dt
amir dt

Reputation: 83

How to get selected item from the listView

I have LinearLayout with a listView (list of the musics) and another LinearLayout with two textView for title and artist of the music files, the list is working properly. My question is how should I define songPicked()(android:onClick="songPicked") method to get the selected music for playback.

Thanks.

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

    }
}

Upvotes: 0

Views: 847

Answers (1)

Nabil
Nabil

Reputation: 672

Try using

listView.setOnItemClickListener(new OnItemClickListener() {

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

        songPicked(position);

    }

});

and change:

public void songPicked(int position){

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

}

Upvotes: 2

Related Questions