0

我对安卓很陌生。我正在尝试使用 arrayadapter 在项目列表上开发搜索功能,如下所示。但搜索无法正常工作。请帮助我

这是我的代码..

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
        }
    });

}
}

这是 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();
}
    }

这是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;
}
   }

这是 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>

这是我的 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>
4

2 回答 2

1

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

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);
}

    }
});
于 2014-03-20T05:41:13.233 回答
0

试试这种方式,这正是你要找的

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // adds item to action bar
        getMenuInflater().inflate(R.menu.search_main, menu);

        // Get Search item from action bar and Get Search service
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
            searchView.setIconified(false);
        }

        return true;
    }

http://androidcss.com/android/android-search-view-php-mysql/

http://androidcss.com/android/fetch-json-data-android/

于 2017-02-07T04:28:28.840 回答