我试图在片段中对listview应用数据绑定,但它不起作用,这是片段的onCreateView方法,其中是膨胀:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SongListBinding songList = DataBindingUtil.inflate(inflater, R.layout.song_list, container, false);
View rootView = inflater.inflate(R.layout.song_list, container, false);
audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
final ArrayList<Song> songs = new ArrayList<Song>();
//Here I'm populating the ArrayList
SongAdapter adapter = new SongAdapter (getActivity(), songs);
songList.list.setAdapter(adapter);
songList.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
releaseMediaPlayer();
Song song = songs.get(position);
int result = audioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaPlayer = MediaPlayer.create(getActivity(), song.getmAudioResourceId());
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
return rootView;
}
但是列表视图没有显示,我不明白为什么,listview xml文件是:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
/></layout>
我也尝试过这样做:
SongListBinding songList = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.song_list, null, false);
但这不起作用
你能帮帮我吗?这是我的SongAdapter java类:
public class SongAdapter extends ArrayAdapter<Song> {
public SongAdapter (Activity context , ArrayList<Song> songs){
super(context, 0,songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Song} object located at this position in the list
Song currentSong = getItem(position);
ListItemBinding listItem = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.list_item, null, false);
//Get the title of the song from the current Song object and
//set this text on the song title TextView
listItem.titleTextView.setText(currentSong.getmSongTitle());
//Get the artist of the song from the current Song object and
//set this text on the song artist TextView
if (currentSong.hasArtist()) {
listItem.artistTextView.setText(currentSong.getmSongArtist());
} else {
listItem.artistTextView.setText(R.string.unkown_artist);
}
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
答案 0 :(得分:0)
我不是100%肯定,但我的猜测是你应该调用父构造函数,如:
public SongAdapter (Activity context, ArrayList<Song> songs){
super(context, R.layout.list_item, songs); // pass layout id like this
}
此外,您不应该两次充气。像这样使用DataBindingUtil:
ListItemBinding listItem = DataBindingUtil.bind(listItemView);
问题可能是错误的绑定数据到未使用的视图,但尝试两种解决方案。希望它有所帮助。