Search code examples
androidlistviewrssandroid-arrayadapterandroid-loadermanager

ListView not displaying anything in new activity using array adapter


I've got a class that should, theoretically, display RSS headlines, from a SINGLE RSS feed site, in a ListView called searchListView. If the ListView is empty a TextView appears informing the user that the ListView is empty, which is what happens every time I navigate to this activity. The button and EditText is for filtering the headlines.

I've got the exact same class in another project (this class is the main activity in said project) which works perfectly fine.

RssItem is a class that parses the RSS feed information into a headline title and URL to the headline.

Note: SearchActivity is not the main activity.

Edit: I'm using a CallBack and LoaderManager in the main activity to display items in a list view as well. I'm not too knowledgeable about these and I really don't know if they might be causing the issue due to the main activity. I do not pass any information from the main activity, though.

FIXED: I wasn't getting any errors or anything but, for some reason, nothing would get displayed. Then I went into the android manifest and realised that the app doesn't have permissions to connect to the internet. Silly mistake, easy to miss.

<uses-permission android:name="android.permission.INTERNET"/>

SearchActivity.java

public class SearchActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<RssItem>> {
    private EditText mEditText;

    private Button mButton;

    private ArrayAdapter<RssItem> mAdapter;
    private ListView mListView ;
    // hard wire Rss feed source for the time being
    private String mDataSource = "http://feeds.bbci.co.uk/news/uk/rss.xml";
    // no search string at the moment
    private String mSearchString = "";

    private static final int LOADER_ID = 1;
    // The callbacks through which we will interact with the LoaderManager.
    private LoaderManager.LoaderCallbacks<List<RssItem>> mCallbacks;
    private LoaderManager mLoaderManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mButton = (Button) findViewById(R.id.button);
        mEditText = (EditText)findViewById(R.id.editTextSearch);
        mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        mListView = (ListView)findViewById(R.id.searchListView);
        mListView.setAdapter(mAdapter);
        TextView emptyText = (TextView)findViewById(R.id.textViewEmpty);
        mListView.setEmptyView(emptyText);
        // Set list view item click listener
        mListView.setOnItemClickListener(new ListListener(this));

        // The Activity (which implements the LoaderCallbacks<Cursor>
        // interface) is the callbacks object through which we will interact
        // with the LoaderManager. The LoaderManager uses this object to
        // instantiate the Loader and to notify the client when data is made
        // available/unavailable.
        mCallbacks = this;

        // Initialize the Loader with id '1' and callbacks 'mCallbacks'.
        // If the loader doesn't already exist, one is created. Otherwise,
        // the already created Loader is reused. In either case, the
        // LoaderManager will manage the Loader across the Activity/Fragment
        // lifecycle, will receive any new loads once they have completed,
        // and will report this new data back to the 'mCallbacks' object.
        mLoaderManager = getLoaderManager();
        mLoaderManager.initLoader(LOADER_ID, null, mCallbacks);
    }

    // handler for search button click
    public void onClick(View v){
        mSearchString = mEditText.getText().toString();
        mLoaderManager.restartLoader(LOADER_ID, null, mCallbacks);
    }

    @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 Loader<List<RssItem>> onCreateLoader(int id, Bundle args) {
        RssLoader loader = new RssLoader(
                this, // context
                mDataSource, // URL of Rss feed
                mSearchString  // search loaded RssItem for match in title
        );
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<List<RssItem>> loader, List<RssItem> data) {
        mAdapter.clear();
        mAdapter.addAll(data);
    }

    @Override
    public void onLoaderReset(Loader<List<RssItem>> loader) {
        mAdapter.clear();
    }
}

ListListener.java

public class ListListener implements OnItemClickListener {
    // And a reference to a calling activity
    // Calling activity reference
    Activity mParent;
    /** We will set those references in our constructor.*/
    public ListListener(Activity parent) {
        mParent  = parent;
    }

    /** Start a browser with url from the rss item.*/
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        // We create an Intent which is going to display data
        Intent i = new Intent(Intent.ACTION_VIEW);
        // We have to set data for our new Intent;
        i.setData(Uri.parse(((RssItem)(parent.getItemAtPosition(pos))).getLink()));
        // And start activity with our Intent
        mParent.startActivity(i);
    }
}

Solution

  • I wasn't getting any errors or anything but, for some reason, nothing would get displayed. Then I went into the android manifest and realised that the app doesn't have permissions to connect to the internet. Silly mistake, easy to miss.

    <uses-permission android:name="android.permission.INTERNET"/> was missing.