【问题标题】:empty view not display in listview空视图不显示在列表视图中
【发布时间】:2017-06-03 09:47:53
【问题描述】:

我正在尝试在没有互联网连接时显示空白视图给我没有互联网连接并在没有找到书时不显示任何书

这是我的代码

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

我想在没有连接时给我没有互联网连接`

 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
  • 您可以为空状态添加另一个布局。并根据列表大小务实地显示/隐藏。
  • 你能发布你的xml吗?

标签: java android listview


【解决方案1】:

试试这个

View  emptyV = getActivity().getLayoutInflater().inflate(R.layout.empty_view, null);
                    TextView tvMSG = (TextView) emptyV.findViewById(R.id.tvMSG);
                    tvMSG.setText("No product available.");
                    ViewGroup viewGroup= (ViewGroup) listView.getParent();                  
                    viewGroup.addView(emptyV, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT)); 
                    listView.setEmptyView(emptyV);  

【讨论】:

    【解决方案2】:

    您可以为empty 状态添加另一个layout,并根据list 的大小实际添加show/hide

    这是一个例子:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <RelativeLayout
            android:id="@+id/layout_empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:visibility="gone"
            android:layout_margin="30dp">
    
            <ImageView
                android:id="@+id/image_empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher"/>
    
            <TextView
                android:id="@+id/text_empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/image_empty"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:textSize="20sp"
                android:textStyle="bold"
                android:textColor="@color/textColorPrimary"
                android:text="Oops!"/>
    
            <TextView
                android:id="@+id/text_empty_hints"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/text_empty"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:textSize="14sp"
                android:textStyle="bold"
                android:textColor="@color/textColorSecondary"
                android:text="No items found"/>
        </RelativeLayout>
    </RelativeLayout>
    

    MainActivity.java:

    private ListView bookListView;
    private RelativeLayout layoutEmpty;
    
    .......
    ............
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        bookListView = (ListView) findViewById(R.id.list_view);
        layoutEmpty = (RelativeLayout) findViewById(R.id.layout_empty);
    
        .......
        ...........
    }
    
    private void updateUi(List<Book> books) {
    
        if(books.size() > 0) {
            layoutEmpty.setVisibility(View.GONE);
            bookListView.setVisibility(View.VISIBLE);
        } else {
            layoutEmpty.setVisibility(View.VISIBLE);
            bookListView.setVisibility(View.GONE);
        }
    }
    

    输出:

    希望对你有帮助~

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2021-09-20
      • 2023-01-04
      • 2018-12-21
      相关资源
      最近更新 更多