Reputation: 8705
I have a existing fragment, in which i want to display typical address kind of information like Street, City, PIN and List of Phone Numbers.
To display List of Phone Numbers, i added listview in addressfragment.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp" >
<TextView
android:id="@+id/txtStreetAddr"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="default" />
<ListView
android:id="@+id/phones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:paddingLeft="0dp" >
</ListView>
</LinearLayout>
</ScrollView>
My fragment class:
public class AddressFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
"123, Corner Street");
ListView phoneListView = ((ListView) rootView.findViewById(R.id.phones));
ArrayList<String> phNumbers = new ArrayList<String>();
phNumbers.add("4343534343");
phNumbers.add("6767566766");
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(MyApp.getAppContext(),android.R.layout.simple_list_item_1, phNumbers);
// Set The Adapter
phoneListView.setAdapter(arrayAdapter);
return rootView;
}
The phone list view just does not show up on the screen - Why? Is there a better way to handle this?
Upvotes: 1
Views: 1734
Reputation: 47
if you've scrollview as parent view then, you've to set listview height programmatically, you can create a method, like below---
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
then pass listview to this method like below --
phoneListView.setAdapter(arrayAdapter);
setListViewHeightBasedOnChildren(phoneListView);
Hope this will work perfectly.
Upvotes: 0
Reputation: 3831
Remove ScrollView
and make the LinearLayout
as the parent layout.
Upvotes: 1
Reputation: 47817
I think you forget to inflate View
in onCreateView(...)
View rootView= inflater.inflate(R.layout.your_layout, container, false);
add your onCreateView(...)
should be
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.your_layout, container, false);
((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
"123, Corner Street");
ListView phoneListView =((ListView) rootView.findViewById(R.id.phones);
ArrayList<String> phNumbers = new ArrayList<String>();
phNumbers.add("4343534343");
phNumbers.add("6767566766");
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, phNumbers);
// Set The Adapter
phoneListView.setAdapter(arrayAdapter);
return rootView;
}
Upvotes: 3