需要清除listview中的适配器

时间:2017-06-03 20:20:56

标签: android arrays listview adapter android-adapter

我试图从列表视图中清除适配器,但没有任何反应 这是我的代码

 @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);
                bookAdapter.clear();

            }
        }

3 个答案:

答案 0 :(得分:0)

在您的代码中Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ClearTabControl(TabControl1) End Sub Private Sub ClearTabControl(ByVal tb As TabControl) For Each page As TabPage In TabControl1.TabPages ClearFields(page) Next End Sub Private Sub ClearFields(ByVal cntr As Control) For Each ctl As Control In cntr.Controls If TypeOf ctl Is TextBox OrElse TypeOf ctl Is ComboBox Then ctl.Text = "" ElseIf ctl.HasChildren Then ClearFields(ctl) End If Next End Sub 包含bookAdapter.clear()之后

答案 1 :(得分:0)

您必须为适配器调用notifyDataSetChanged()方法,该方法将通知适配器数据已更改,并且反映数据集的任何视图都应自行刷新。

答案 2 :(得分:0)

@mohammedragabmohammedborik,这是互联网逻辑的一个例子。

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
    // you are online and do your stuff
} else {
    // not online, show MessageBox or something else
} 

将此权限添加到AndroidManifest.xml:

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

我无法帮助您adapter.clear(),您还没有提供适配器代码。