【问题标题】:How to get selected item from the listView如何从 listView 中获取所选项目
【发布时间】:2017-10-17 13:16:54
【问题描述】:

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

    }
}

【问题讨论】:

    标签: java android android-layout listview


    【解决方案1】:

    尝试使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      相关资源
      最近更新 更多