ListView 使用数组适配器在新 activity 中不显示任何内容
ListView not displaying anything in new activity using array adapter
我有一个 class,理论上应该在名为 searchListView
的 ListView 中显示来自单个 RSS 提要站点的 RSS 标题。如果 ListView 为空,则会出现一个 TextView,通知用户 ListView 为空,这就是我每次导航到此 activity 时发生的情况。按钮和EditText用于过滤标题。
我在另一个项目中得到了完全相同的 class(这个 class 是所述项目中的主要 activity),它工作得很好。
RssItem 是一个 class 将 RSS 提要信息解析为标题并 URL 为标题。
注意:SearchActivity 不是主要activity。
编辑:我也在主 activity 中使用 CallBack 和 LoaderManager 来在列表视图中显示项目。我对这些不太了解,我真的不知道它们是否可能由于主要 activity 而导致问题。不过,我没有传递来自主要 activity 的任何信息。
已修复:我没有收到任何错误或任何内容,但由于某种原因,什么也不会显示。然后我进入 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);
}
}
我没有收到任何错误或任何信息,但由于某种原因,没有显示任何内容。然后我进入 android 清单并意识到该应用程序没有连接到互联网的权限。愚蠢的错误,容易错过。
<uses-permission android:name="android.permission.INTERNET"/>
丢失。
我有一个 class,理论上应该在名为 searchListView
的 ListView 中显示来自单个 RSS 提要站点的 RSS 标题。如果 ListView 为空,则会出现一个 TextView,通知用户 ListView 为空,这就是我每次导航到此 activity 时发生的情况。按钮和EditText用于过滤标题。
我在另一个项目中得到了完全相同的 class(这个 class 是所述项目中的主要 activity),它工作得很好。
RssItem 是一个 class 将 RSS 提要信息解析为标题并 URL 为标题。
注意:SearchActivity 不是主要activity。
编辑:我也在主 activity 中使用 CallBack 和 LoaderManager 来在列表视图中显示项目。我对这些不太了解,我真的不知道它们是否可能由于主要 activity 而导致问题。不过,我没有传递来自主要 activity 的任何信息。
已修复:我没有收到任何错误或任何内容,但由于某种原因,什么也不会显示。然后我进入 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);
}
}
我没有收到任何错误或任何信息,但由于某种原因,没有显示任何内容。然后我进入 android 清单并意识到该应用程序没有连接到互联网的权限。愚蠢的错误,容易错过。
<uses-permission android:name="android.permission.INTERNET"/>
丢失。