0
我想顯示空視圖的時候沒有互聯網連接的給我沒有互聯網連接,並顯示沒有一本書時,沒有發現任何書空視圖不顯示在列表視圖
這是我的代碼
public class MainActivity extends AppCompatActivity {
private static final String BOOK_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?q=";
public static final String LOG_TAG = MainActivity.class.getName();
// search button
private Button search_button;
// edit text to use for search
private EditText editText;
// string used to get string from edit text.
private String queryWord = null;
private TextView emptyTextView;
private ListView bookListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search_button = (Button) findViewById(R.id.next_button);
editText = (EditText) findViewById(R.id.edit_text);
emptyTextView = (TextView) findViewById(R.id.empty_view);
bookListView = (ListView) findViewById(R.id.list_view);
// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
在這裏我要在不連接給我沒有互聯網connection`
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (networkInfo != null && networkInfo.isConnected()) {
new GetBook().execute(BOOK_REQUEST_URL);
} else {
emptyTextView.setText(R.string.NoConnect);
bookListView.setEmptyView(emptyTextView);
}
}
});
new GetBook().execute(BOOK_REQUEST_URL);
}
// method to get string from edit text.
public String getBook() {
queryWord = editText.getText().toString();
if (queryWord == null) {
bookListView.setEmptyView(emptyTextView);
}
return queryWord;
}
/**
* Update the UI with the given book information.
*/
private void updateUi(List<Book> books) {
// Find a reference to the {@link ListView} in the layout
// Create a new {@link ArrayAdapter} of book
final BookAdapter adapter = new BookAdapter(this, books);
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
bookListView.setAdapter(adapter);
}
// inner class for to support methods do in the background.
private class GetBook extends AsyncTask<String, Void, List<Book>> {
@Override
protected List<Book> doInBackground(String... urls) {
String word_search = getBook();
// Don't perform the request if there are no URLs, or the first URL is null.
if (urls.length < 1 || urls[0] == null) {
return null;
}
List<Book> result = QueryUtils.fetchBookName(urls[0], word_search);
return result;
}
/**
* This method is invoked on the main UI thread after the background work has been
* completed.
* <p>
* It IS okay to modify the UI within this method. We take the {@link Book} object
* (which was returned from the doInBackground() method) and update the views on the screen.
*/
這裏我不想顯示列表書
@Override
protected void onPostExecute(List<Book> book) {
emptyTextView.setText(R.string.nobook);
bookListView.setEmptyView(emptyTextView);
// Hide loading indicator because the data has been loaded
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
if ((book != null) && !book.isEmpty()) {
updateUi(book);
}
}
}
}
我嘗試更多的步驟沒有發生
爲什麼你會顯示適配器空的文本?簡單地檢查結果是否爲空,然後隱藏'listView'並顯示一些'textView',在xml中添加帶有'listView'的textView –
您可以爲空狀態添加另一個佈局。並根據列表大小務實地顯示/隱藏。 – FAT
你可以請張貼你的XML嗎? – FAT