2017-06-03 72 views
0

清除适配器我想从列表视图中明确适配器,但没有发生 这里是我的代码需要从列表视图

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

      } 
     } 
+0

你的Adapter的实现是错误的...... Fx你是从ArrayAdapter扩展并重写getCount但不清楚。常见的错误是将数据再次存储在自定义ArrayAdapter中,而ArrayAdapter已经存储了数据。 – Selvin

+0

你需要阅读很多:https://stackoverflow.com/questions/24775171/the-clear-in-custom-listview-adapter-also-updates-the-list-data – Stanojkovic

回答

0

bookAdapter.clear()之后包括bookAdapter.notifyDataSetChanged()在您的代码中

+0

当按按钮搜索应用程序崩溃时,不互联网连接 – mohammedragabmohammedborik

+1

您检查状态,但如果没有互联网,您需要编写逻辑。 – Stanojkovic

+0

@Stanojkovic你可以给我举例 – mohammedragabmohammedborik

0

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

+0

当按下按钮搜索应用程序崩溃的时候没有互联网connectio – mohammedragabmohammedborik

+0

添加崩溃日志请 –

+0

06-03 23:17:51.232 5965-5965/com.example.mohammedragab.booklisting E/AndroidRuntime:致命异常:主要 过程:com.example.mohammedragab.booklisting,PID:5965 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.example.mohammedragab.booklisting.BookAdapter.clear()' this给我错误 – mohammedragabmohammedborik

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(),你没有提供适配器代码。

+0

感谢您的报价最好 但我在我的代码中做了这个defenition – mohammedragabmohammedborik