我在片段中创建了一个Listview,但是当我执行应用程序时,ListView没有出现在屏幕上。没有崩溃。我有一个textview和listview。 Textview显示但listview不显示。
片段代码:
public class PlayerFragment extends Fragment {
private List<Station> myStation = new ArrayList<Station>();
private LayoutInflater mInflater;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_player,
null);
populateStationList();
myStation.add(new Station("station1", "url1", R.drawable.station1, "station1 description"));
myStation.add(new Station("station2", "url2", R.drawable.station2, "station2 description"));
myStation.add(new Station("station3", "url3", R.drawable.station3, "station3 description"));
populateListView();
ArrayAdapter<Station> adapter = new MyListAdapter();
ListView list = (ListView) view.findViewById(R.id.stationListView);
list.setAdapter(adapter);
registerClickCallback();
list = (ListView) view.findViewById(R.id.stationListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
Station clickedStation = myStation.get(position);
String message = "You clicked position " + position
+ "\n" + "Station: " + clickedStation.getStationName();
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
});
return inflater.inflate(R.layout.fragment_player, container, false);
}
private class MyListAdapter extends ArrayAdapter<Station> {
public MyListAdapter() {
super(getActivity(), R.layout.station_list_view, myStation);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = mInflater.inflate(R.layout.station_list_view, parent, false);
}
// Find the car to work with.
Station currentStation = myStation.get(position);
// Fill the view
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentStation.getStationAlbum());
TextView stationName = (TextView) itemView.findViewById(R.id.item_station_name_text_view);
stationName.setText(currentStation.getStationName());
TextView stationURL = (TextView) itemView.findViewById(R.id.item_url_text_view);
stationURL.setText("" + currentStation.getStationURL());
TextView stationDescription = (TextView) itemView.findViewById(R.id.item_description_text_view);
stationDescription.setText(currentStation.getStationDescription());
return itemView;
}
}
}
片段的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playerFragment"
tools:context="com.brandonong.sgpradio.PlayerFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="heysdadasdasdadasdasd"
android:id="@+id/textview1"/>
<ListView
android:id="@+id/stationListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1" >
</ListView>
</RelativeLayout>
答案 0 :(得分:0)
问题是,除了布局膨胀inflater.inflate(R.layout.fragment_player, container, false);
之外,所有其他代码都应放在onViewCreated
内。
试试这个,看看它是否有效!
编辑:
你的onCreateView:
返回inflater.inflate(R.layout.fragment_player,container,false);
OnViewCreated:
populateStationList();
myStation.add(new Station("station1", "url1", R.drawable.station1, "station1 description"));
myStation.add(new Station("station2", "url2", R.drawable.station2, "station2 description"));
myStation.add(new Station("station3", "url3", R.drawable.station3, "station3 description"));
populateListView();
ArrayAdapter<Station> adapter = new MyListAdapter();
ListView list = (ListView) view.findViewById(R.id.stationListView);
list.setAdapter(adapter);
registerClickCallback();
list = (ListView) view.findViewById(R.id.stationListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
Station clickedStation = myStation.get(position);
String message = "You clicked position " + position
+ "\n" + "Station: " + clickedStation.getStationName();
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
});
顺便说一句,我真的建议你看看这个post来查看ViewHolder模式,这样可以大大改善你的ListView性能!