我想通过edittext找出指定用户。但是edittext是ListView的第一行。 我怎样才能做到这一点?
public class ChatFragment extends Fragment {
private ListView chatListView;
private List<ChatItem> items;
private ChatAdapter chatAdapter;
public static ChatFragment newInstance(){
ChatFragment fragment = new ChatFragment();
return fragment;
}
@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View view = inflater.inflate(R.layout.chat_fragment, container, false);
chatListView = (ListView)view.findViewById(R.id.chatListView);
setupChatList();
chatListView.setTextFilterEnabled(true);
return view;
}
public void setupChatList(){
items = new ArrayList<>();
items.add(new ChatItem(1, "","","",""));
items.add(new ChatItem(2 ,"layer1", "Chloe Grace Moretz", "Approved your see post request!", "4 mins"));
items.add(new ChatItem(3 ,"layer2", "Robert Downey", "ok!", "16:19"));
items.add(new ChatItem(4 ,"layer3", "Amanda Crew", "haha", "19:01"));
items.add(new ChatItem(5 ,"layer4", "Noah Urrea", "Good choice ~ man", "Yesterday"));
items.add(new ChatItem(6 ,"layer5", "Blake Rayne", "Jason Papadopoulos I think", "Sunday"));
chatAdapter = new ChatAdapter(getContext(), items);
chatListView.setAdapter(chatAdapter);
}
public void remove(){
chatAdapter.remove(items.get(1));
}
}
我的适配器如下:
public class ChatAdapter extends ArrayAdapter<ChatItem> {
private List<ChatItem> items;
private List<ChatItem> filteredData = null;
private TextView chatNameTxt;
private TextView msgTxt;
private TextView msgTimeTxt;
private ImageView chatImg;
private ImageView chatMoreImg;
private Context context;
private ItemFilter filter = new ItemFilter();
private EditText findChat;
public ChatAdapter(Context context, List<ChatItem> items){
super(context, -1, items);
this.items = items;
this.context = context;
}
@Override public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position==0){
View item0 = inflater.inflate(R.layout.chat_list_view_item0, parent, false);
findChat = (EditText)item0.findViewById(R.id.findChat);
findChat.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
ChatFragment chatFragment = new ChatFragment();
chatFragment.remove();
}
});
return item0;
}else {
View item = inflater.inflate(R.layout.chat_list_view, parent, false);
chatNameTxt = (TextView) item.findViewById(R.id.chatNameTxt);
msgTxt = (TextView) item.findViewById(R.id.msgTxt);
msgTimeTxt = (TextView) item.findViewById(R.id.msgTimeTxt);
chatImg = (ImageView)item.findViewById(R.id.chatImg);
chatMoreImg = (ImageView)item.findViewById(R.id.chatMoreImg);
ChatItem friendItem = items.get(position);
String chatName = friendItem.getUserNmae();
String msg = friendItem.getLastMsg();
String msgTime = friendItem.getLastMsgTime();
String userImg = friendItem.getUserImageName();
chatImg.setImageResource(context.getResources().getIdentifier(userImg, "drawable", context.getPackageName()));
chatNameTxt.setText(chatName);
msgTxt.setText(msg);
msgTimeTxt.setText(msgTime);
return item;
}
}
}
答案 0 :(得分:1)
在Filter
上输入ListView
EditText
时,您已按照以下方式操作。
TextWatcher
添加到EditText#addTextChangedListener
onTextChanged
中添加或删除ListView
适配器中的项目。如果您是ArrayAdapter
的子类,则会有add
和remove
方法BaseAdapter
的子类,请创建BaseAdapter implements Filterable
。注意:第2步:适用于
ArrayAdapter
,第3步适用于BaseAdapter
。
答案 1 :(得分:0)
您可以使适配器 Filterable 。它会自动更新listview的值。
我明天会在这里写教程,并保留链接以表彰作者。
答案 2 :(得分:0)
无需创建额外的过滤器类。您只需保留TextWatcher的字符串,然后使用contains方法
将其与列表视图数据一起检查searchforcategory.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) {
getAllCetegory(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
现在只需检查数据中的字符串
for(int i=0;i<response.body().getDataList().size();i++){
if(response.body().getDataList().get(i).getName().contains(checkString)){
productDataLists.add(response.body().getDataList().get(i));
}
}