我的应用程序使用横向全屏模式和导航抽屉.我正在listView我的应用程序中使用edittext.这edittext是搜索的搜索栏listview.导航抽屉中的listview和edittext都是.但是,当没有与搜索到的单词匹配的列表项时,它将listview变为空.
那么如何添加"找不到项目"消息而不是空白listview?
我在互联网上搜索了很多,发现了一种方法,setEmptyView();但我无法理解,因此无法正常工作.请帮我!也许这个问题已在这里提出,但请给我一个简单的解释.
这是我的代码:
MainActivity.java
public class MainActivity extends FragmentActivity {
final String[] data = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium"};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
//the code below will automatically close the keyboard when the user will touch the listview
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="@+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="@+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="240dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="@+id/searchbar"
android:layout_width="230dp"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="@drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint=" search" >
</EditText>
<TextView
android:id="@+id/notfound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@android:style/TextAppearance.Medium"
android:gravity="center">
</TextView>
<ListView android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
是的,您已经正确地提到了 setEmptyView() ,如果您想在 ListView 变空时显示空消息,则应该使用它。
现在这里有一个 xml 布局和代码描述了如何准确使用 setEmptyView。
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layoutTitlebar" >
<ListView
android:id="@+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/friendBGColor"
android:cacheColorHint="#00000000" >
</ListView>
<TextView
android:id="@+id/empty"
style="@android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/strNoRecordsFound" >
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在,您必须使用以下方法将此空视图(即 TextView)设置为 ListView:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
// set your adapter here
// set your click listener here
// or whatever else
listViewFriends.setEmptyView(findViewById(R.id.empty));
Run Code Online (Sandbox Code Playgroud)