Reputation: 3
So. I'm pretty noob in fragments, and i spent my last days tryin to show a very very simple RSS Reader in an app which has many fragments. I was following a guide (actually many guides, but this one was the one which had less code.. just for learning purpose: http://www.html.it/pag/19519/android-e-le-applicazioni-di-rete/).
This is the xml about the fragment in which i would to show the RSS Reader (called fragment_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#607D8B"
tools:context=".MainActivity$PlaceholderFragment"
android:id="@+id/main1">
<ImageView
android:id="@+id/newsBanner1"
android:layout_width="wrap_content"
android:layout_height="120sp"
android:layout_marginTop="0dp"
android:gravity="top"
android:layout_marginBottom="10dp"
android:src="@drawable/news_banner"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News:"
android:id="@+id/newsTitleText1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="140dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ScrollView
android:id="@+id/scrollerNews1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/newsTitleText1"
android:layout_marginTop="8dp"
android:scrollbars="vertical"
android:fillViewport="true">
<ListView
android:id="@+id/rssListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
Yeah it has "main1" id because is the "default" layout fragment at the opening of the app. And this is the java class
public class NewsFragment extends Fragment {
String feedUrl = "";
ListView rssListView = null;
ArrayList<RSSItem> RSSItems = new ArrayList<RSSItem>();
ArrayAdapter<RSSItem> array_adapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View setView = inflater.inflate(R.layout.fragment_main, container, false);
feedUrl = "sample_feed_url";
refreshRSSList();
rssListView = (ListView) setView.findViewById(R.id.rssListView);
array_adapter = new ArrayAdapter<RSSItem>(this.getActivity(), R.layout.fragment_main, RSSItems);
rssListView.setAdapter(array_adapter);
refreshRSSList();
return setView;
}
private void refreshRSSList() {
ArrayList<RSSItem> newItems = RSSItem.getRSSItems(feedUrl);
RSSItems.clear();
RSSItems.addAll(newItems);
}
At the moment when i run the app, everything is showed except for the listview (which, i guess, is empty).
Many thanks to whoever will help me :D
Upvotes: 0
Views: 728
Reputation: 1422
When you initialize your array adapter, the layout parameter is wrong. You have mentioned the layout of the fragment. Here it's asking the layout of listview. I mean layout of an item in listview. You need to create a new file in layout and name it as list_item.xml. Define list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
</TextView>
Now your array adapter will be
array_adapter =new ArrayAdapter(getActivity(),R.layout.list_item,R.id.list_item_textview,rssItems);
You can also use the constructor which you have defined. What I did explicitly mentions id of TextView. You can see the difference here: link. Let me know if it doesn't work.
Upvotes: 1