我有一个项目,我可以添加数据,删除它们并使用SQLite更新它们。当然我在列表视图中显示它们。我想做的最后一件事是使用searchView对象,因此用户可以搜索他想要的数据并检查它们是否在那里。但是SearchView对象什么都不做。我按目的键入了列表视图中存在的内容,并且匹配的行不会出现。
这是我的代码。
public class DisplayForldersActivity extends AppCompatActivity {
DatabaseHandler dba;
private ArrayList<MyFolder> dbFolders = new ArrayList<>();
private FolderAdapter folderAdapter;
private ListView listView;
SearchView inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_forlders);
listView = (ListView)findViewById(R.id.list);
inputSearch = (SearchView) findViewById(R.id.inputSearch);
fetchDataFromDB();
}
private void fetchDataFromDB() {
dbFolders.clear();
dba = new DatabaseHandler(getApplicationContext());
ArrayList<MyFolder> foldersFromDB = dba.getFolderDetails();
for(int i=0; i<foldersFromDB.size();i++){
String plateNo = foldersFromDB.get(i).getPlateNumber();
String owner = foldersFromDB.get(i).getOwnerName();
String cardId = foldersFromDB.get(i).getCardId();
String content = foldersFromDB.get(i).getContent();
String date = foldersFromDB.get(i).getRecordDate();
int mid = foldersFromDB.get(i).getItemId();
MyFolder f = new MyFolder();
f.setPlateNumber(plateNo);
f.setOwnerName(owner);
f.setCardId(cardId);
f.setContent(content);
f.setRecordDate(date);
f.setItemId(mid);
dbFolders.add(f);
folderAdapter = new FolderAdapter(DisplayForldersActivity.this,R.layout.folders_row,dbFolders);
listView.setAdapter(folderAdapter);
folderAdapter.notifyDataSetChanged();
inputSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
folderAdapter.getFilter().filter(newText);
return false;
}
});
}
dba.close();
}
我的适配器代码
private class FolderAdapter extends ArrayAdapter<MyFolder>{
Activity activity;
int layoutResource;
MyFolder myFolder;
ArrayList<MyFolder> mData = new ArrayList<>();
public FolderAdapter(Activity act, int resource, ArrayList<MyFolder> data) {
super(act, resource,data);
activity = act;
layoutResource = resource;
mData = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public MyFolder getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null || row.getTag()==null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource,null);
holder = new ViewHolder();
holder.mPlateNo = (TextView)row.findViewById(R.id.plateNumberList);
//holder.mOwner = (TextView)row.findViewById(R.id.ownerName);
holder.mcardId = (TextView)row.findViewById(R.id.idNumber);
//holder.mContent = (TextView)row.findViewById(R.id.processing);
holder.mDate = (TextView)row.findViewById(R.id.dateText);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.myF = getItem(position);
holder.mPlateNo.setText(holder.myF.getPlateNumber());
//holder.mOwner.setText(holder.myF.getOwnerName());
//holder.mcardId.setText(holder.myF.getCardId());
//holder.mPlateNo.setText(holder.myF.getPlateNumber());
holder.mDate.setText(holder.myF.getRecordDate());
final ViewHolder finalHolder = holder;
holder.mPlateNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dateText = finalHolder.myF.getRecordDate().toString();
String owner = finalHolder.myF.getOwnerName().toString();
String cardId = finalHolder.myF.getCardId().toString();
String plateNumber = finalHolder.myF.getCardId().toString();
String content = finalHolder.myF.getContent().toString();
int mid = finalHolder.myF.getItemId();
Intent i = new Intent(DisplayForldersActivity.this,DetailedFolderActivity.class);
i.putExtra("id",mid);
i.putExtra("owner",owner);
i.putExtra("cardId",cardId);
i.putExtra("plateNumber",plateNumber);
i.putExtra("content",content);
i.putExtra("dateText",dateText);
startActivity(i);
}
});
return row;
}
class ViewHolder{
MyFolder myF;
int mid;
TextView mPlateNo;
TextView mOwner;
TextView mcardId;
TextView mContent;
TextView mDate;
}
}
我做错了什么吗?感谢
答案 0 :(得分:0)
您需要在附加到listView的适配器中调用notifyDataSetChanged以使ListView无效并使其绘制已更改/删除/插入的行。
答案 1 :(得分:0)
您的数组适配器需要实现Filterable接口,它将覆盖getFilter()方法。然后在你的getFilter()方法中,你可以执行过滤操作。
查看此stackoverflow链接,此代码可能会帮助您 No results with custom ArrayAdapter Filter
答案 2 :(得分:0)
SerchView不处理任何搜索,它只是一个小部件。您必须自己实现搜索逻辑。试试这里描述的解决方案: http://developer.android.com/guide/topics/search/search-dialog.html
您也可以通过调用SearchView.setOnQueryTextListener(SearchView.OnQueryTextListener侦听器)并传入侦听器的自定义实现来执行此操作。