ListView不使用数组适配器在新活动中显示任何内容

时间:2016-04-22 00:17:56

标签: android listview rss android-arrayadapter android-loadermanager

我有一个类,理论上应该从一个名为searchListView的ListView中的SINGLE RSS提要站点显示RSS标题。如果ListView为空,则会显示TextView,通知用户ListView为空,这是每次导航到此活动时发生的情况。按钮和EditText用于过滤标题。

我在另一个项目中有完全相同的类(这个类是所述项目中的主要活动),它完全正常。

RssItem 是一个将RSS提要信息解析为标题标题和标题网址的类。

注意:SearchActivity 不是主要活动。

编辑:我在主活动中使用CallBack和LoaderManager来显示列表视图中的项目。我不太了解这些,我真的不知道它们是否可能因主要活动而导致问题。但是,我没有从主要活动中传递任何信息。

修复:我没有收到任何错误或其他任何错误,但由于某种原因,什么都不会显示。然后我进入了android清单,意识到该应用程序没有连接到互联网的权限。愚蠢的错误,容易错过。

<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);
    }
}

1 个答案:

答案 0 :(得分:1)

我没有得到任何错误或任何东西,但由于某种原因,什么都不会显示。然后我进入了android清单,意识到该应用程序没有权限连接到互联网。愚蠢的错误,容易错过。

<uses-permission android:name="android.permission.INTERNET"/>失踪了。