我正在尝试实现仅在一个片段中需要的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(),但都没有解决问题。
最佳答案
像这样更改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(....)
关于android - Android SearchView无法分段工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28170242/