Android SearchView не работает во фрагменте
Я пытаюсь реализовать searchview, который требуется только в одном фрагменте. Он будет фильтровать по списку.
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(), и ни один из них не решил проблему.
2 ответа
Решение
+ Изменить 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 как показано ниже вместе с фильтром для просмотра списка, как в ссылке 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;
}
});