gpt4 book ai didi

java - 如何从listView中获取选定的项目

转载 作者:行者123 更新时间:2023-12-02 02:33:04 24 4
gpt4 key购买 nike

我有一个带有 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();

}
}

最佳答案

尝试使用

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

}

关于java - 如何从listView中获取选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46791157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com