想通过查询EditText的数据,更改搜索框下面的TextView的显示列表,TextView的数据是用SqlLite查出来显示在另一个item
要怎么写才可以让EditText输入数据,通过SqlLite查询数据,用适配器实时改变下方的TextView显示的数据列表。
不能实现这个功能是我把EditText和TextView都放在一个ListView中,如果单独把EditText拿到ListView外面就能实现,图中就是一整个ListView的内容,上面是一个搜索框,下面是每个用户的用户名和头像。
设配器要怎么写
public class MyAdapter extends BaseAdapter {
private ArrayList<Contact> contacts ;
//ArrayList<Contact> contacts;
Context context;
private static final int TYPE_A = 0;
//itemB类的type标志
private static final int TYPE_B = 1;
public MyAdapter(Context context, ArrayList<Contact> contacts){
this.contacts=contacts;
this.context =context;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
int p=position;
if (p==0){
return TYPE_A;
}else {
return TYPE_B;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder1 holder1 =null;
ViewHolder2 holder2 =null;
int type =getItemViewType(position);
if (view ==null){
holder1 =new ViewHolder1();
holder2 =new ViewHolder2();
switch (type){
case TYPE_A:
//这是holder2,是搜索框
view =View.inflate(parent.getContext(),R.layout.search,null);
holder2.ibtCancel =view.findViewById(R.id.ibtCancel);
holder2.etSearch =view.findViewById(R.id.etSearch);
view.setTag(R.id.tag_second,holder2);
break;
case TYPE_B:
//这是holder1,搜索框下的list_item
view =View.inflate(parent.getContext(),R.layout.list_item,null);
holder1.listView_item =view.findViewById(R.id.listView_item);
holder1.imgName =view.findViewById(R.id.imaName);
holder1.tvName =view.findViewById(R.id.tvName);
view.setTag(R.id.tag_first,holder1);
break;
}
}else {
switch (type){
case TYPE_A:
holder2 =(ViewHolder2) view.getTag(R.id.tag_second);
break;
case TYPE_B:
holder1 =(ViewHolder1) view.getTag(R.id.tag_first);
break;
}
}
switch (type){
case TYPE_A:
ViewHolder2 finalHolder = holder2;
holder2.etSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {//搜索框焦点操作
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){//得到焦点的操作
finalHolder.etSearch.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) {
}
});
Toast.makeText(parent.getContext(),"获得焦点",Toast.LENGTH_LONG).show();
}else {
}
}
});
break;
case TYPE_B:
TextView tv_list =view.findViewById(R.id.tvName);
tv_list.setText(contacts.get(position).getName());//设置name
break;
}
return view;
}
private static class ViewHolder1 {//list_item
DrawImage imgName;
TextView tvName;
LinearLayout listView_item;
}
private static class ViewHolder2 {//seach
ImageButton ibtCancel;
EditText etSearch;
}
}