卷不过的叫混子 2021-05-15 17:04 采纳率: 0%
浏览 29
已结题

listView中怎么通过EditText,实时更改下方数据

想通过查询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;

    }
}
  • 写回答

1条回答 默认 最新

  • 卷不过的叫混子 2021-05-15 17:08
    关注
    public class DBControl {
        public  MyHelper myHelper;
    
    
        public DBControl(Context context){
            myHelper =new MyHelper(context);
        }
    
        /**
         *   增
         * @param values
         * @return  result结果为1,或0
         */
        public long insertContact(ContentValues values){
    
            SQLiteDatabase db = myHelper.getWritableDatabase();//创建数据库
    
            long result= db.insert("linhaitians",null,values);
            db.close();
            return  result;
        }
    
        /**
         *  查  根据姓名
         * @param name 联系人姓名
         * @return 联系人对象
         */
        public  ArrayList<Contact> queryContact(String name){
            ArrayList<Contact> contactquery =new ArrayList<>();
            SQLiteDatabase db =myHelper.getReadableDatabase();
            Cursor cursor= db.query("linhaitians",null,"name=?", new String[]{name},null,null,null);//游标
            while (cursor.moveToNext()){//通过姓名查询电话和邮箱
                int rid= cursor.getInt(0);
                String rname = cursor.getString(1);//姓名
                String rcompany =cursor.getString(2);//公司
                String rphone = cursor.getString(3);//手机
                String remail = cursor.getString(3);//邮箱
                String remakes =cursor.getString(4);//备注
                Contact contact =new Contact(rid,rname,rcompany,rphone,remail,remakes);
                contactquery.add(contact);
            }
            cursor.close();
            db.close();
            return contactquery;
        }
    
        /**
         * list显示query
         * @return
         */
        public  ArrayList<Contact> query(){
            ArrayList<Contact> contactslist =new ArrayList<>();
            SQLiteDatabase db =myHelper.getReadableDatabase();
            Cursor cursor =db.query("linhaitians",null,null,null,null,null,null);
            while (cursor.moveToNext()){
                int rid= cursor.getInt(cursor.getColumnIndex("id"));
                String rname = cursor.getString(cursor.getColumnIndex("name"));//姓名
                String rcompany =cursor.getString(cursor.getColumnIndex("company"));
                String rphone = cursor.getString(cursor.getColumnIndex("phone"));
                String remail = cursor.getString(cursor.getColumnIndex("email"));
                String remakes =cursor.getString(cursor.getColumnIndex("remakes"));
                Contact contact =new Contact(rid,rname,rcompany,rphone,remail,remakes);
                contactslist.add(contact);
            }
            cursor.close();
            db.close();
            return contactslist;
        }
    
        /**
         *  删
         * @param id
         * @return  删除成功返回1,失败返回0
         */
        public int deleteContact(String id){
    
            SQLiteDatabase db =myHelper.getWritableDatabase();
            int res= db.delete("linhaitians","id=?",new String[]{id});
            db.close();
            return res;
        }
    
        /**
         *  更新
         * @param values
         * @return result1 返回结果1||0
         */
        public  long updateContact(ContentValues values){  //可以传一个ContentValues对象
    
            SQLiteDatabase db =myHelper.getWritableDatabase();
    
            long result1= db.update("linhaitians",values,"id=?", new String[]{values.get("id")+""});
            return result1;
        }
        
    }
    

    数据库控制类

    评论

报告相同问题?

悬赏问题

  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
  • ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战