Java 如何启用搜索功能从Listview中查找所有值?
我正在androidstudio中为Android构建一个应用程序,并试图在一个列表视图中找到一个字符串值,该列表视图由基于云的SQL服务器填充。我正在使用Android 8.0的API 26。到目前为止,它正在发挥作用并找到一些价值 问题-当在emulator上访问我的最后一个(第三个)活动并在搜索字段中插入字符串值时,它找不到所有内容和整个值。 例如,我有一个充满值的列表(苹果、橘子、红苹果、葡萄等等)。当搜索苹果产品时,会发生的情况是,在键入“ap”后,它会找到它,并在键入“app”后立即消失。另外,在尝试查找橙色时,键入第一个字母o时,它不会显示任何内容 就像它没有在地图中找到所有的值一样 我已尝试在适配器中调试循环。它似乎正确理解了约束,获得了“i”的正确计数,并找到了包含特定约束的所有结果 我的适配器:Java 如何启用搜索功能从Listview中查找所有值?,java,sql,loops,listview,search,Java,Sql,Loops,Listview,Search,我正在androidstudio中为Android构建一个应用程序,并试图在一个列表视图中找到一个字符串值,该列表视图由基于云的SQL服务器填充。我正在使用Android 8.0的API 26。到目前为止,它正在发挥作用并找到一些价值 问题-当在emulator上访问我的最后一个(第三个)活动并在搜索字段中插入字符串值时,它找不到所有内容和整个值。 例如,我有一个充满值的列表(苹果、橘子、红苹果、葡萄等等)。当搜索苹果产品时,会发生的情况是,在键入“ap”后,它会找到它,并在键入“app”后立即
package com.example.user.sortiment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Created by User
*/
public class SortimentAdapter extends BaseAdapter {
LayoutInflater mInflator;
List<Sortiment> map;
List<Sortiment> filterMap;
public void performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
if (Objects.equals(filterString, "")) {
filterMap.clear();
filterMap.addAll(map);
notifyDataSetChanged();
return;
}
int count = map.size();
filterMap.clear();
for (int i = 0; i < count; i++) {
Sortiment filterableSortiment = map.get(i);
if (filterableSortiment.name.toLowerCase().contains(filterString)) {
filterMap.add(filterableSortiment);
}
}
notifyDataSetChanged();
}
public SortimentAdapter(Context c, List<Sortiment> inputMap) {
mInflator = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
filterMap = new ArrayList<>();
filterMap.addAll(inputMap);
map = new ArrayList<>();
map.addAll(inputMap);
}
@Override
public int getCount() {
return filterMap.size();
}
@Override
public Object getItem(int position) {
return filterMap.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Sortiment viewObject = filterMap.get(position);
viewHolder.nameTextView.setText(viewObject.name);
viewHolder.priceTextView.setText(viewObject.ean.toString());
return convertView;
}
private class ViewHolder {
TextView nameTextView;
TextView priceTextView;
public ViewHolder(View view) {
nameTextView = (TextView)view.findViewById(R.id.nameTextView);
priceTextView = (TextView) view.findViewById(R.id.priceTextView);
}
}
}
package com.example.user.sortiment;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.Filter;
导入android.widget.Filterable;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入java.util.Objects;
/**
*由用户创建
*/
公共类SortimentAdapter扩展BaseAdapter{
拉平机;
列表地图;
列表过滤器映射;
公共空性能过滤(字符序列约束){
String filterString=constraint.toString().toLowerCase();
if(Objects.equals(filterString,“”){
filterMap.clear();
filterMap.addAll(映射);
notifyDataSetChanged();
返回;
}
int count=map.size();
filterMap.clear();
for(int i=0;i
我的活动:
package com.example.user.sortiment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class SortimentActivity extends AppCompatActivity {
List<Sortiment> sortimentMap = new ArrayList<>();
SortimentAdapter sortimentAdapter;
Context thisContext;
EditText edtSearch;
ListView myListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sortiment);
Integer selectedStoreId = getIntent().getExtras().getInt("store_id");
edtSearch = (EditText)findViewById(R.id.edtSearch);
myListView = (ListView) findViewById(R.id.storesListView);
thisContext = this;
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (sortimentAdapter != null){
sortimentAdapter.performFiltering(charSequence);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
GetSortimentForStore retrieveData = new GetSortimentForStore();
retrieveData.execute(selectedStoreId);
}
public void performFiltering(CharSequence constraint) {
List<Sortiment> filteredList = new ArrayList<>();
String filterString = constraint.toString().toLowerCase();
if (Objects.equals(filterString, "")) {
filteredList.addAll(sortimentMap);
sortimentAdapter.notifyDataSetChanged();
return;
}
int count = sortimentMap.size();
filteredList.clear();
for (int i = 0; i < count; i++) {
Sortiment filterableSortiment = sortimentMap.get(i);
if (filterableSortiment.name.toLowerCase().contains(filterString)) {
filteredList.add(filterableSortiment);
}
}
sortimentAdapter = new SortimentAdapter(thisContext, filteredList);
sortimentAdapter.notifyDataSetChanged();
}
private class GetSortimentForStore extends AsyncTask<Integer,String,String> {
String msg = "";
// JDBC Driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// Example 10.20.30.40:3306
static final String DB_URL = "jdbc:mysql://" +
DbStrings.DATABASE_URL + "/" +
DbStrings.DATABASE_NAME;
@Override
protected void onPreExecute() {
//progressTextView.setText("Connecting...");
}
@Override
protected String doInBackground(Integer... selectedStores) {
Connection conn = null;
Statement stmt = null;
try {
Integer selectedStore = selectedStores[0];
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);
stmt = conn.createStatement();
String sql = "select id, store_id, name, ean, description from myproject.sortiments WHERE store_id = " + selectedStore;
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Sortiment newSortiment = new Sortiment();
newSortiment.id = rs.getInt("id");
newSortiment.name = rs.getString("name");
newSortiment.store_id = rs.getInt("store_id");
newSortiment.ean = rs.getString("ean");
newSortiment.description = rs.getString("description");
sortimentMap.add(newSortiment);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException connError) {
msg = "An exception was thrown for JDBC.";
connError.printStackTrace();
} catch (ClassNotFoundException e) {
msg = "A class not found exception was thrown.";
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String msg) {
sortimentAdapter = new SortimentAdapter(thisContext, sortimentMap);
myListView.setAdapter(sortimentAdapter);
}
}
}
package com.example.user.sortiment;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.SearchView;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Objects;
公共类SortimentActivity扩展了AppCompatActivity{
List sortimentMap=new ArrayList();
SortimentAdapter SortimentAdapter;
语境这一语境;
编辑文本搜索;
列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u sortiment);
整数selectedStoreId=getIntent().getExtras().getInt(“存储id”);
edtSearch=(EditText)findViewById(R.id.edtSearch);
myListView=(ListView)findViewById(R.id.storesListView);
这个上下文=这个;
edtSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i1,int i2){
if(sortimentAdapter!=null){
sortimentAdapter.性能过滤(charSequence);
}
}
@凌驾
public void PostTextChanged(可编辑){
}
});
GetSortimentForStore retrieveData=新建GetSortimentForStore();
retrieveData.execute(selectedStoreId);
}
公共空性能过滤(字符序列约束){
List filteredList=新建ArrayList();
String filterString=constraint.toString().toLowerCase();
if(Objects.equals(filterString,“”){
filteredList.addAll(sortimentMap);
sortimentAdapter.notifyDataSetChanged();
返回;
}
int count=sortimentMap.size();
filteredList.clear();
for(int i=0;i <activity android:name=".SortimentActivity"
android:windowSoftInputMode="stateHidden">
</activity>