在Android的搜索功能在列表视图 [英] Search functionality Within List View in Android

查看:107
本文介绍了在Android的搜索功能在列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty的新机器人。我试图通过使用arrayadapter如下所示,开发项目的列表上的搜索功能。但搜索无法正常工作。 PLZ plz帮助我

下面是我的code ..

  AddToOutlet.java
       公共类AddToOutlet延伸活动{
       是SessionManager会议;
       字符串的成功,cus_id,bus_id,cus_outlet;
       ArrayList的<项目>项目;
       com.amplio.MyCustomEditText编辑搜索;

// TextView的tvcountoutlet;

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.addtooutlet);
    会议=新SessionManager(中间getApplicationContext());
    session.checkLoginback();

    项目=新的ArrayList<项目>();
    //从会话的用户数据
    HashMap的<字符串,字符串>用户= session.getUserDetails();
    // ID
    最后弦乐cus_id = user.get(SessionManager.KEY_ID);


    ArrayList的<的NameValuePair> postParameters =新的ArrayList<的NameValuePair>();
    postParameters.add(新BasicNameValuePair(cus_id,cus_id));
    串响应=无效;
    尝试 {
        响应= LoginHttpClient
                .executeHttpPost(
                        http://10.0.2.2/android_api/add_to_outlet.php
                        postParameters);
        响应= response.toString();
        响应= response.replaceAll(\\ S +,);
        JSONObject的JSON =新的JSONObject(响应);
        JSONArray jArray = json.getJSONArray(客户);
        的for(int i = 0; I< jArray.length();我++){
            的JSONObject json_data = jArray.getJSONObject(ⅰ);
            项目是=新项目();
            成功= json_data.getString(成功);
            it.setBusname(json_data.getString(bus_name));
            it.setBusid(json_data.getString(bus_id));
            it.setCusId(cus_id);
            item.add(它);
        }
    }赶上(例外五){
        e.printStackTrace();
    }

    ListView的LV =(ListView控件)findViewById(R.id.list);
    最后MyAdapter适配器=新MyAdapter(AddToOutlet.this,
            R.layout.addtooutlet_list_item,项目);
    lv.setAdapter(适配器);

    //找到的EditText在listview_main.xml
    编辑搜索=(com.amplio.MyCustomEditText)findViewById(R.id.EditText01);

    在EditText上//捕获文本
    editsearch.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效afterTextChanged(编辑为arg0){
            // TODO自动生成方法存根
            字符串文本= editsearch.getText()的toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(文本);
        }

        @覆盖
        公共无效beforeTextChanged(CharSequence的arg0中,INT ARG1,
                INT ARG2,诠释ARG3){
            // TODO自动生成方法存根
        }

        @覆盖
        公共无效onTextChanged(CharSequence的arg0中,诠释ARG1,INT ARG2,
                INT ARG3){
            // TODO自动生成方法存根
        }
    });

}
}
 

下面是MyAdapter.java

 公共类MyAdapter扩展ArrayAdapter<项目> {
    ArrayList的<项目>项目;
     LayoutInflater mInflater;
   上下文语境;
    私人列表<项目> ITEM1 = NULL;

公共MyAdapter(上下文的背景下,INT资源的ArrayList<项目>项目){
    超(背景下,资源,项目);
    this.item =项目;
    this.context =背景;
    mInflater = LayoutInflater.from(上下文);

}

@覆盖
公众诠释getCount将(){
    返回item.size();
}

@覆盖
公共项目的getItem(INT位置){
    返回item.get(位置);
}

@覆盖
众长getItemId(INT位置){
    返回的位置;
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    最后ViewHolder持有人;
    如果(convertView == NULL){
        持有人=新ViewHolder();
        convertView = mInflater.inflate(R.layout.addtooutlet_list_item,
                父母,假);
        holder.tv1 =(TextView中)convertView.findViewById(R.id.item_bname);
        holder.tv2 =(TextView中)convertView.findViewById(R.id.item_bid);
        holder.tv3 =(TextView中)convertView.findViewById(R.id.item_cid);
        holder.b =(按钮)convertView.findViewById(R.id.item_button);
        convertView.setTag(保持器);
    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }
    项目IT = item.get(位置);
    holder.tv1.setText(it.getBusname());
    holder.tv2.setText(it.getBusid());
    holder.tv3.setText(it.getCusId());
    holder.b.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            //字符串Text1_value = holder.tv1.getText()的toString()。
            //的System.out.println(Text1_value);
            串投标= holder.tv2.getText()的toString()。
            的System.out.println(BID);
            。字符串CID = holder.tv3.getText()的toString();
            的System.out.println(CID);
            holder.b.setText(加入);
            holder.b.setTextColor(Color.parseColor(#FFFFFF));
            holder.b.setTextSize(10);
            // holder.b.setText(加入);
            // Toast.makeText(背景下,参加单击按钮,
            // Toast.LENGTH_LONG).show();
            ArrayList的<的NameValuePair> postParameters =新的ArrayList<的NameValuePair>();
            postParameters.add(新BasicNameValuePair(cus_id,CID));
            postParameters.add(新BasicNameValuePair(bus_id,BID));
            串响应=无效;
            尝试 {
                响应= LoginHttpClient
                        .executeHttpPost(
                                http://10.0.2.2/android_api/update_outlet.php
                                postParameters);
                字符串解析度= response.toString();
                RES = res.replaceAll(\\ S +,);
                INT resInt =的Integer.parseInt(RES);
                如果(resInt == 1){
                } 其他 {
                }
            }赶上(例外五){
            }
        }
    });
    返回convertView;
}

静态类ViewHolder {
    按钮B;
    TextView的TV1,TV2,TV3;
}

公共无效过滤器(字符串charText){
    charText = charText.toLowerCase(Locale.getDefault());
    item.clear();
    如果(charText.length()== 0){
        item.addAll(项目);
    } 其他 {
        为(项目WP:项目){
            如果(wp.getBusname()。与toLowerCase(Locale.getDefault())
                    。载(charText)){
                item.add(WP);
            }
        }
    }
    notifyDataSetChanged();
}
    }
 

下面是Item.java

 公共类项目{

字符串busname,busid,cus_id;

公共字符串getBusname(){
    返回busname;
}

公共无效setBusname(字符串busname){
    this.busname = busname;
}

公共字符串getBusid(){
    返回busid;
}

公共无效setBusid(字符串busid){
    this.busid = busid;
}

公共字符串getCusId(){
    返回cus_id;
}

公共无效setCusId(字符串cus_id){
    this.cus_id = cus_id;
}
   }
 

下面是addtooutlet.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:后台=#FFFFFF
            机器人:方向=垂直>

        <! -  Editext的搜索 - >

        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =1DP
            机器人:layout_marginLeft =10dp
            机器人:后台=#FFFFFF>

            < com.amplio.MyCustomEditText
                机器人:ID =@ + ID / EditText01
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =中心
                机器人:layout_marginLeft =10dp
                机器人:layout_marginRight =40dp
                机器人:后台=#00000000
                机器人:EMS =20
                机器人:提示=@字符串/搜索
                机器人:lineSpacingExtra =5DP
                机器人:TEXTSIZE =12SP>
            < /com.amplio.MyCustomEditText>
        < / RelativeLayout的>

        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginLeft =10dp
            机器人:layout_marginRight =10dp
            机器人:后台=#EEEEEE>

            <的ListView
                机器人:ID =@ + ID /列表
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =1
                机器人:drawSelectorOnTop =FALSE/>
        < / RelativeLayout的>
    < / LinearLayout中>
 

这是我的addtooutlet_list_item.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
         机器人:layout_width =match_parent
         机器人:layout_height =46dp
          机器人:后台=#FFFFFF
         机器人:填充=5dip
         机器人:weightSum =100>

    <的TextView
        机器人:ID =@ + ID / item_bname
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =20
        机器人:填充=2DP
        机器人:文本=你好
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =12dip/>

    <的TextView
        机器人:ID =@ + ID / item_bid
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =20
        机器人:填充=2DP
        机器人:文本=你好
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =12dip
        机器人:能见度=隐形/>

    <的TextView
        机器人:ID =@ + ID / item_cid
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =20
        机器人:填充=2DP
        机器人:文本=你好
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =12dip
        机器人:能见度=隐形/>

    <按钮
        机器人:ID =@ + ID / item_button
        机器人:layout_width =31dp
        机器人:layout_height =31dp
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / item_bname
        机器人:背景=@可绘制/圈
        机器人:可聚焦=假
        机器人:focusableInTouchMode =假
        机器人:文本=加盟
        机器人:文字颜色=#FFFFFF
        机器人:TEXTSIZE =10SP/>
< / RelativeLayout的>
 

解决方案

尝试做这样的。希望它会帮助你......

  editsearch.addTextChangedListener(新TextWatcher(){

    @覆盖
    公共无效afterTextChanged(编辑为arg0){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效beforeTextChanged(CharSequence的arg0中,INT ARG1,
            INT ARG2,诠释ARG3){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效onTextChanged(CharSequence的arg0中,诠释ARG1,INT ARG2,
            INT ARG3){
        // TODO自动生成方法存根
        editsearch.requestFocus();
        最后的ArrayList<项目> itemArray =新的ArrayList<项目>();
        INT长度限制:Textlength = 0;
        。=长度限制:Textlength editsearch.getText()长度();
        itemArray.clear();
        的for(int i = 0; I< item.size();我++)
        {
            串itemTitle = item.get(ⅰ)。名称();
            字符串compareItemTitle =;
            如果(长度限制:Textlength&其中; = item.length())
            {
               。compareItemTitle =(字符串)itemTitle.toLowerCase()子进程(0,长度限制:Textlength);
               如果(compareItemTitle.contentEquals(editsearch.getText()的toString()。与toLowerCase()))
               {
                    itemArray.add(item.get(ⅰ));
               }
            }
        }
        适配器=新MyAdapter(AddToOutlet.this,R.layout.addtooutlet_list_item,itemArray);
        lv.setAdapter(适配器);
}

    }
});
 

I'm pretty new to android. I'm trying to develop a search function on a list of items by using arrayadapter as shown below. but search is not working correctly. plz plz help me

Here is my code..

AddToOutlet.java 
       public class AddToOutlet extends Activity {
       SessionManager session;
       String success, cus_id, bus_id, cus_outlet;
       ArrayList<Item> item;
       com.amplio.MyCustomEditText editsearch;

// TextView tvcountoutlet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtooutlet);
    session = new SessionManager(getApplicationContext());
    session.checkLoginback();

    item = new ArrayList<Item>();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    // ID
    final String cus_id = user.get(SessionManager.KEY_ID);


    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("cus_id", cus_id));
    String response = null;
    try {
        response = LoginHttpClient
                .executeHttpPost(
                        "http://10.0.2.2/android_api/add_to_outlet.php",
                        postParameters);
        response = response.toString();
        response = response.replaceAll("\\s+", "");
        JSONObject json = new JSONObject(response);
        JSONArray jArray = json.getJSONArray("customer");
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            Item it = new Item();
            success = json_data.getString("success");
            it.setBusname(json_data.getString("bus_name"));
            it.setBusid(json_data.getString("bus_id"));
            it.setCusId(cus_id);
            item.add(it);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    ListView lv = (ListView) findViewById(R.id.list);
    final MyAdapter adapter = new MyAdapter(AddToOutlet.this,
            R.layout.addtooutlet_list_item, item);
    lv.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (com.amplio.MyCustomEditText) findViewById(R.id.EditText01);

    // Capture Text in EditText
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }
    });

}
}

Here is MyAdapter.java

       public class MyAdapter extends ArrayAdapter<Item> {
    ArrayList<Item> item;
     LayoutInflater mInflater;
   Context context;
    private List<Item> item1 = null;

public MyAdapter(Context context, int resource, ArrayList<Item> item) {
    super(context, resource, item);
    this.item = item;
    this.context = context;
    mInflater = LayoutInflater.from(context);

}

@Override
public int getCount() {
    return item.size();
}

@Override
public Item getItem(int position) {
    return item.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.addtooutlet_list_item,
                parent, false);
        holder.tv1 = (TextView) convertView.findViewById(R.id.item_bname);
        holder.tv2 = (TextView) convertView.findViewById(R.id.item_bid);
        holder.tv3 = (TextView) convertView.findViewById(R.id.item_cid);
        holder.b = (Button) convertView.findViewById(R.id.item_button);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Item it = item.get(position);
    holder.tv1.setText(it.getBusname());
    holder.tv2.setText(it.getBusid());
    holder.tv3.setText(it.getCusId());
    holder.b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // String Text1_value = holder.tv1.getText().toString();
            // System.out.println(Text1_value);
            String bid = holder.tv2.getText().toString();
            System.out.println(bid);
            String cid = holder.tv3.getText().toString();
            System.out.println(cid);
            holder.b.setText("Joined");
            holder.b.setTextColor(Color.parseColor("#ffffff"));
            holder.b.setTextSize(10);
            // holder.b.setText("joined");
            // Toast.makeText(context, "Join button Clicked",
            // Toast.LENGTH_LONG).show();
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("cus_id", cid));
            postParameters.add(new BasicNameValuePair("bus_id", bid));
            String response = null;
            try {
                response = LoginHttpClient
                        .executeHttpPost(
                                "http://10.0.2.2/android_api/update_outlet.php",
                                postParameters);
                String res = response.toString();
                res = res.replaceAll("\\s+", "");
                int resInt = Integer.parseInt(res);
                if (resInt == 1) {
                } else {
                }
            } catch (Exception e) {
            }
        }
    });
    return convertView;
}

static class ViewHolder {
    Button b;
    TextView tv1, tv2, tv3;
}

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    item.clear();
    if (charText.length() == 0) {
        item.addAll(item);
    } else {
        for (Item wp : item) {
            if (wp.getBusname().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                item.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}
    }

Here is Item.java

  public class Item {

String busname, busid, cus_id;

public String getBusname() {
    return busname;
}

public void setBusname(String busname) {
    this.busname = busname;
}

public String getBusid() {
    return busid;
}

public void setBusid(String busid) {
    this.busid = busid;
}

public String getCusId() {
    return cus_id;
}

public void setCusId(String cus_id) {
    this.cus_id = cus_id;
}
   }

Here is addtooutlet.xml

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:orientation="vertical" >

        <!-- Editext for Search -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="10dp"
            android:background="#ffffff" >

            <com.amplio.MyCustomEditText
                android:id="@+id/EditText01"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="40dp"
                android:background="#00000000"
                android:ems="20"
                android:hint="@string/search"
                android:lineSpacingExtra="5dp"
                android:textSize="12sp" >
            </com.amplio.MyCustomEditText>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#eeeeee" >

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawSelectorOnTop="false" />
        </RelativeLayout>
    </LinearLayout>

here is my addtooutlet_list_item.xml

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="46dp"
          android:background="#ffffff"
         android:padding="5dip"
         android:weightSum="100" >

    <TextView
        android:id="@+id/item_bname"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:padding="2dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="12dip" />

    <TextView
        android:id="@+id/item_bid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:padding="2dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="12dip"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/item_cid"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:padding="2dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="12dip"
        android:visibility="invisible" />

    <Button
        android:id="@+id/item_button"
        android:layout_width="31dp"
        android:layout_height="31dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/item_bname"
        android:background="@drawable/circle"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="join"
        android:textColor="#ffffff"
        android:textSize="10sp" />
</RelativeLayout>

解决方案

Try to do like this. hope it will help you...

editsearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub
        editsearch.requestFocus();
        final ArrayList<Item> itemArray = new ArrayList<Item>();
        int textlength = 0;
        textlength = editsearch.getText().length();
        itemArray.clear();
        for (int i = 0; i < item.size(); i++)
        {
            String itemTitle = item.get(i).name();
            String compareItemTitle = "";
            if (textlength <= item.length())
            {
               compareItemTitle = (String) itemTitle.toLowerCase().subSequence(0, textlength);
               if(compareItemTitle.contentEquals(editsearch.getText().toString().toLowerCase()))
               {
                    itemArray.add(item.get(i));
               }
            }
        }
        adapter = new MyAdapter(AddToOutlet.this, R.layout.addtooutlet_list_item, itemArray);
        lv.setAdapter(adapter);
}

    }
});

这篇关于在Android的搜索功能在列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆