我有一个带有listView(音乐列表)的LinearLayout和另一个带有两个textView的LinearLayout,用于标题和艺术家
的音乐文件,列表工作正常。我的问题是如何定义songPicked()
(android:onClick="songPicked"
)方法来获取所选音乐以进行播放。
感谢。
<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();
}
}
答案 0 :(得分:2)
尝试使用
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
songPicked(position);
}
});
并改变:
public void songPicked(int position){
Toast.makeText(getApplicationContext(), "clicked on "+position, Toast.LENGTH_LONG).show();
}