我正在跟踪有关Udemy的视频指南,该指南介绍了如何为学校项目创建Spotify克隆Android应用程序,当我尝试通过ListView在应用程序中显示歌曲时,它不起作用。我已经将我的代码与视频课程的代码进行了三重检查,结果相同,但是我的代码没有显示在应用程序上。
content_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="umsl.edu.umslplay.MainActivity"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/SongsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
songlistview_row.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/songsRowTextView"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp" />
</LinearLayout>
适配器代码
public class SongListAdapter extends BaseAdapter {
private Activity activity;
private List<Song> songs;
private static LayoutInflater inflater = null;
public SongListAdapter (Activity a, List<Song> s) {
activity = a;
songs = s;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount () {
return songs.size();
}
@Override
public Object getItem(int position) {
return null;
}
public long getItemId (int position) {
return position;
}
public View getView (int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null)
v = inflater.inflate(R.layout.songlistview_row, parent, false);
TextView title = (TextView)v.findViewById(R.id.songsRowTextView);
Song song = songs.get(position);
title.setText(song.getTitle());
return v;
}
}
主要活动代码
public class MainActivity extends AppCompatActivity {
static FloatingActionButton playPauseButton;
PlayerService mBoundService;
boolean mServiceBound = false;
List<Song> songs = new ArrayList<>();
ListView songsListView;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
PlayerService.MyBinder myBinder = (PlayerService.MyBinder) service;
mBoundService = myBinder.getService();
mServiceBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
}
};
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isPlaying = intent.getBooleanExtra("isPlaying", false);
flipPlayPauseButton(isPlaying);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
playPauseButton = findViewById(R.id.fab);
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mServiceBound)
mBoundService.togglePlayer();
}
});
songsListView = (ListView) findViewById(R.id.SongsListView);
fetchSongsFromWeb();
}
private void startStreamingService (String url){
Intent i = new Intent(this, PlayerService.class);
i.putExtra("url", url);
i.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(i);
bindService(i, mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mServiceBound) {
unbindService(mServiceConnection);
mServiceBound = false;
}
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("changePlayButton"));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}
public static void flipPlayPauseButton (boolean isPlaying) {
if (isPlaying) {
playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
}else {
playPauseButton.setImageResource(android.R.drawable.ic_media_play);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void fetchSongsFromWeb () {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
URL url = new
URL("https://umslmusicapp.com/music_app/getmusic.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
inputStream = new
BufferedInputStream((urlConnection.getInputStream()));
String response = convertInputStreamToString (inputStream);
Log.i("GOT SONGS!", response);
parseIntoSongs (response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection !=null)
urlConnection.disconnect();
}
}
});
thread.start();
}
private String convertInputStreamToString (InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) !=null) {
result += line;
}
if (inputStream != null)
inputStream.close();
return result;
}
private void parseIntoSongs (String data) {
String[] dataArray = data.split("\\*");
int i = 0;
for (i = 0; i < dataArray.length; i++) {
String[] songArray = dataArray[i].split(",");
Song song = new Song(songArray[0], songArray[1], songArray[2], songArray[3]);
songs.add(song);
}
for (i = 0; i < songs.size(); i++) {
Log.i("GOT SONG", songs.get(i).getTitle());
}
populateSongsListView();
}
private void populateSongsListView () {
runOnUiThread(new Runnable() {
@Override
public void run() {
SongListAdapter adapter = new SongListAdapter(MainActivity.this, songs);
songsListView.setAdapter(adapter);
songsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Song song = songs.get(position);
String songAddress = "http://umslmusicapp.com/music_app/" + song.getTitle();
startStreamingService(songAddress);
}
});
}
});
}
}
歌曲列表应该通过ListView显示,但应用程序上什么都没有显示
编辑:
Song.Java代码
public class Song {
int Id;
String title;
int numplays;
int numlikes;
public Song (String Id, String title, String numplays, String numlikes) {
try {
this.Id = Integer.parseInt(Id);
}
catch (Exception e){
this.Id = 0;
}
this.title = title;
this.numplays = Integer.parseInt(numplays);
this.numlikes = Integer.parseInt(numlikes);
}
public int getId (){
return Id;
}
public String getTitle () {
return title;
}
public int getNumplays(){
return numplays;
}
public int getNumlikes (){
return numlikes;
}
}