Android SearchView 在片段中不工作
Android SearchView not working in fragment
我正在尝试实现一个仅在一个片段中需要的搜索视图。它将在列表视图上进行过滤。
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import com.alcontrol.atmis.anticimex.R;
import com.alcontrol.atmis.anticimex.model.ListViewsModel;
import java.util.ArrayList;
import java.util.HashMap;
public class SalesParts extends Fragment {
public SalesParts() {
// Required empty public constructor
}
ArrayList<ListViewsModel> listofOrderSelectedArray = new ArrayList<ListViewsModel>();
ArrayList<ListViewsModel> listofOrdersArray = new ArrayList<ListViewsModel>();
SimpleAdapter adapter;
ArrayAdapter arrayAdapter2;
ListView mListView;
ArrayList<HashMap<String, Object>> items;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listofOrdersArray.add(new ListViewsModel(101, "Order Title 1",""));
listofOrdersArray.add(new ListViewsModel(102, "Order Title 2",""));
listofOrdersArray.add(new ListViewsModel(103, "Order Title 3",""));
listofOrdersArray.add(new ListViewsModel(104, "Order Title 4",""));
listofOrdersArray.add(new ListViewsModel(105, "Order Title 5",""));
listofOrdersArray.add(new ListViewsModel(106, "Order Title 6",""));
listofOrdersArray.add(new ListViewsModel(107, "Order Title 7",""));
listofOrdersArray.add(new ListViewsModel(108, "Order Title 8",""));
listofOrdersArray.add(new ListViewsModel(109, "Order Title 9",""));
listofOrdersArray.add(new ListViewsModel(110, "SOrder Title 10",""));
mListView = (ListView) getActivity().findViewById(R.id.salespartsList);
mListView.setTextFilterEnabled(true);
SearchView mSearchView = (SearchView) getActivity().findViewById(R.id.typeFindSalesPart);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
items = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> listItem;
for (int i = 0; i < listofOrdersArray.size(); i++) {
ListViewsModel selectedDescription = listofOrdersArray.get(i);
listItem = new HashMap<String, Object>();
listItem.put("itemTitle",selectedDescription.itemTitle);
listItem.put("itemID",selectedDescription.itemId);
listItem.put("addBtn", "");
items.add(listItem);
}
ListView addSelectedItems = (ListView) getActivity().findViewById(R.id.salespartsListSelected);
arrayAdapter2 = new ArrayAdapter(getActivity(),R.layout.selected_salespart_item,R.id.itemTitle, listofOrderSelectedArray) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// get filled view from SimpleAdapter
View itemView = super.getView(position, convertView, parent);
// find our button there
View addButton = itemView.findViewById(R.id.itemDeleteBtn);
// set a tag to get button position in array (used to get values when button is clicked
addButton.setTag(String.valueOf(position));
// add an onClickListener
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tagName = (String) v.getTag();
int arrayPosition = Integer.valueOf(tagName);
//remove selected item from the list of sales parts to choose from
listofOrderSelectedArray.remove(arrayPosition);
arrayAdapter2.notifyDataSetChanged();
}
});
return itemView;
}
};
addSelectedItems.setAdapter(arrayAdapter2);
adapter = new SimpleAdapter(getActivity(), items, R.layout.salespart_item, new String[]{"itemTitle", "addBtn"}, new int[]{R.id.itemTitle, R.id.itemAddBtn}) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// get filled view from SimpleAdapter
View itemView = super.getView(position, convertView, parent);
// find our button there
View addButton = itemView.findViewById(R.id.itemAddBtn);
// set a tag to get button position in array (used to get values when button is clicked
addButton.setTag(String.valueOf(position));
// add an onClickListener
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tagName = (String) v.getTag();
int arrayPosition = Integer.valueOf(tagName);
String title =listofOrdersArray.get(arrayPosition).itemTitle;
int id =listofOrdersArray.get(arrayPosition).itemId;
// add selected item to list of selected sales parts
listofOrderSelectedArray.add(new ListViewsModel(id, title, ""));
arrayAdapter2.notifyDataSetChanged();
}
});
return itemView;
}
};
mListView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sales_parts, container, false);
}
}
我已将以上代码放在 'OnActivityCreated' 我在 setOnQueryTextListener(this) 行收到错误消息。我已尝试将 'this' 更改为 getActivity() 和 getView(),但都没有解决问题。
改变 onCreateView(...)
喜欢
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_sales_parts, container, false);
mListView = (ListView)view.findViewById(R.id.salespartsList);
SearchView mSearchView = (SearchView)view.findViewById(R.id.typeFindSalesPart);
...........
return view;
}
并删除 onActivityCreated(....)
中的所有代码并移至 onCreateView(....)
为什么不像下面那样使用 searchView 以及 link Android SearchView Filter ListView
中的列表视图过滤器
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// filter listview
return true;
}
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
});
我正在尝试实现一个仅在一个片段中需要的搜索视图。它将在列表视图上进行过滤。
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import com.alcontrol.atmis.anticimex.R;
import com.alcontrol.atmis.anticimex.model.ListViewsModel;
import java.util.ArrayList;
import java.util.HashMap;
public class SalesParts extends Fragment {
public SalesParts() {
// Required empty public constructor
}
ArrayList<ListViewsModel> listofOrderSelectedArray = new ArrayList<ListViewsModel>();
ArrayList<ListViewsModel> listofOrdersArray = new ArrayList<ListViewsModel>();
SimpleAdapter adapter;
ArrayAdapter arrayAdapter2;
ListView mListView;
ArrayList<HashMap<String, Object>> items;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listofOrdersArray.add(new ListViewsModel(101, "Order Title 1",""));
listofOrdersArray.add(new ListViewsModel(102, "Order Title 2",""));
listofOrdersArray.add(new ListViewsModel(103, "Order Title 3",""));
listofOrdersArray.add(new ListViewsModel(104, "Order Title 4",""));
listofOrdersArray.add(new ListViewsModel(105, "Order Title 5",""));
listofOrdersArray.add(new ListViewsModel(106, "Order Title 6",""));
listofOrdersArray.add(new ListViewsModel(107, "Order Title 7",""));
listofOrdersArray.add(new ListViewsModel(108, "Order Title 8",""));
listofOrdersArray.add(new ListViewsModel(109, "Order Title 9",""));
listofOrdersArray.add(new ListViewsModel(110, "SOrder Title 10",""));
mListView = (ListView) getActivity().findViewById(R.id.salespartsList);
mListView.setTextFilterEnabled(true);
SearchView mSearchView = (SearchView) getActivity().findViewById(R.id.typeFindSalesPart);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
items = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> listItem;
for (int i = 0; i < listofOrdersArray.size(); i++) {
ListViewsModel selectedDescription = listofOrdersArray.get(i);
listItem = new HashMap<String, Object>();
listItem.put("itemTitle",selectedDescription.itemTitle);
listItem.put("itemID",selectedDescription.itemId);
listItem.put("addBtn", "");
items.add(listItem);
}
ListView addSelectedItems = (ListView) getActivity().findViewById(R.id.salespartsListSelected);
arrayAdapter2 = new ArrayAdapter(getActivity(),R.layout.selected_salespart_item,R.id.itemTitle, listofOrderSelectedArray) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// get filled view from SimpleAdapter
View itemView = super.getView(position, convertView, parent);
// find our button there
View addButton = itemView.findViewById(R.id.itemDeleteBtn);
// set a tag to get button position in array (used to get values when button is clicked
addButton.setTag(String.valueOf(position));
// add an onClickListener
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tagName = (String) v.getTag();
int arrayPosition = Integer.valueOf(tagName);
//remove selected item from the list of sales parts to choose from
listofOrderSelectedArray.remove(arrayPosition);
arrayAdapter2.notifyDataSetChanged();
}
});
return itemView;
}
};
addSelectedItems.setAdapter(arrayAdapter2);
adapter = new SimpleAdapter(getActivity(), items, R.layout.salespart_item, new String[]{"itemTitle", "addBtn"}, new int[]{R.id.itemTitle, R.id.itemAddBtn}) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// get filled view from SimpleAdapter
View itemView = super.getView(position, convertView, parent);
// find our button there
View addButton = itemView.findViewById(R.id.itemAddBtn);
// set a tag to get button position in array (used to get values when button is clicked
addButton.setTag(String.valueOf(position));
// add an onClickListener
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tagName = (String) v.getTag();
int arrayPosition = Integer.valueOf(tagName);
String title =listofOrdersArray.get(arrayPosition).itemTitle;
int id =listofOrdersArray.get(arrayPosition).itemId;
// add selected item to list of selected sales parts
listofOrderSelectedArray.add(new ListViewsModel(id, title, ""));
arrayAdapter2.notifyDataSetChanged();
}
});
return itemView;
}
};
mListView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sales_parts, container, false);
}
}
我已将以上代码放在 'OnActivityCreated' 我在 setOnQueryTextListener(this) 行收到错误消息。我已尝试将 'this' 更改为 getActivity() 和 getView(),但都没有解决问题。
改变 onCreateView(...)
喜欢
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_sales_parts, container, false);
mListView = (ListView)view.findViewById(R.id.salespartsList);
SearchView mSearchView = (SearchView)view.findViewById(R.id.typeFindSalesPart);
...........
return view;
}
并删除 onActivityCreated(....)
中的所有代码并移至 onCreateView(....)
为什么不像下面那样使用 searchView 以及 link Android SearchView Filter ListView
中的列表视图过滤器searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// filter listview
return true;
}
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
});