MainActivity代码
public class emp_list_activity extends AppCompatActivity {
ArrayList <String> listview_array,items_code;
DatabaseHelper mDB;
ListView employee_list_View;
SearchView search_view_j;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emp_list_activity);
mDB = new DatabaseHelper(this);
search_view_j = findViewById(R.id.search_view);
employee_list_View = findViewById(R.id.employee_list);
listViewData();
}
private class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = getLayoutInflater().inflate(R.layout.checkable_list_layout,null);
return v;
}
}
public void listViewData(){
listview_array = new ArrayList<>();
items_code = new ArrayList<>();
Cursor res = mDB.get_emp_info();
if(res.getCount()!=0){
while (res.moveToNext()){
listview_array.add(res.getString(2));
items_code.add(res.getString(1));
}
}
myAdapter = new MyAdapter(emp_list_activity.this,R.layout.checkable_list_layout,listview_array);
search_view_j.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
myAdapter.getFilter().filter(newText);
return false;
}
});
employee_list_View.setAdapter(myAdapter);
}
}
checkablelistlayout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/list_tv"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/list_tv"
android:id="@+id/list_tv_code"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delete_btn"
android:theme="@style/AppTheme.blue"
android:layout_marginLeft="10dp"
android:textAllCaps="false"
android:text="edit"
android:id="@+id/edit_btn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:theme="@style/AppTheme.red"
android:textAllCaps="false"
android:layout_alignParentRight="true"
android:text="Delete"
android:id="@+id/delete_btn"/>
</RelativeLayout>
未获得正确的输出。我不知道什么searchview正在搜索数据。我在arrayadapter中给出一个名称列表,并搜索名称,但没有得到输出。所以我要先搜索文本,然后显示第一行,然后在输出中不显示任何内容。