[英]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. 我有一个带有listView(音乐列表)的LinearLayout和另一个带有两个textView(用于音乐文件的标题和艺术家)的LinearLayout,该列表正常工作。 My question is how should I define songPicked()
( android:onClick="songPicked"
) method to get the selected music for playback. 我的问题是我应该如何定义songPicked()
( android:onClick="songPicked"
)方法来获取选定的音乐进行播放。
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();
}
}
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.