列表视图结果仅显示OnTextChanged

时间:2013-08-02 02:23:18

标签: android listview search filter android-edittext

我的listView附加到editText以允许用户搜索listView。问题是,填充我的listView的数据仅在用户将文本输入editText后显示,我希望在用户输入任何内容之前显示结果。

public class Search extends Activity {

EditText editText;
ListView listview;
int textlength = 0;
int width;
Button list;
Button filter;
CustomAdapter adapter;
Location userLoc;


private ArrayList<String> brand;
private ArrayList<String> size;
private ArrayList<String> price;
private ArrayList<Integer> percent;
private ArrayList<String> objID;
private ArrayList<String> store;
private ArrayList<String> address;
private ArrayList<Integer> distance;


ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<String> size_sort = new ArrayList<String>();
ArrayList<String> price_sort = new ArrayList<String>();
ArrayList<Integer> percent_sort = new ArrayList<Integer>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
ArrayList<String> id_sort = new ArrayList<String>();
ArrayList<String> store_sort = new ArrayList<String>();
ArrayList<String> address_sort = new ArrayList<String>();
ArrayList<Integer> distance_sort = new ArrayList<Integer>();

Integer longitude;
Integer latitude;


ParseObject dealsObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_page);

    Parse.initialize(this, "vUz23Z6zdIL1jbxbVWeLpsSdu1ClTu3YiG30zTWY",
            "4BTyoq1QQKows8qVJV7lvU3ZokSRrLFyOCPzffwJ");



    listview = (ListView) findViewById(R.id.listView1);
    editText = (EditText) findViewById(R.id.editText1);

    brand = new ArrayList<String>();
    size = new ArrayList<String>();
    price = new ArrayList<String>();
    percent = new ArrayList<Integer>();
    objID = new ArrayList<String>();
    store = new ArrayList<String>();
    address = new ArrayList<String>();
    distance = new ArrayList<Integer>();


    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    userLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = (int) userLoc.getLongitude();
            latitude = (int) userLoc.getLatitude();
            userLoc.setLatitude(latitude);
            userLoc.setLongitude(longitude);
        }


        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub


        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
    getDeals();


    adapter = new CustomAdapter(brand, size, price, percent, objID, store,
            address, distance);

    listview.setAdapter(adapter);


    editText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            textlength = editText.getText().length();
            text_sort.clear();
            size_sort.clear();
            price_sort.clear();
            percent_sort.clear();
            image_sort.clear();
            id_sort.clear();
            store_sort.clear();
            address_sort.clear();
            distance_sort.clear();

            for (int i = 0; i < brand.size(); i++) {
                if (textlength <= brand.get(i).length()) {
                    if (editText
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) brand.get(i).subSequence(0,
                                            textlength))) {

                        text_sort.add(brand.get(i));
                        size_sort.add(size.get(i));
                        price_sort.add(price.get(i));
                        percent_sort.add(percent.get(i));
                        id_sort.add(objID.get(i));
                        store_sort.add(store.get(i));
                        address_sort.add(address.get(i));
                        distance_sort.add(distance.get(i));


                        listview.setAdapter(new CustomAdapter(text_sort,
                                size_sort, price_sort, percent_sort,
                                id_sort, store_sort, address_sort,
                                distance_sort));

                        // Register onClickListener to Handle Click Events
                        // on Each Item
                        listview.setOnItemClickListener(new OnItemClickListener() {
                            //
                            // Argument position gives the index of item
                            // which is clicked
                            @Override
                            public void onItemClick(AdapterView<?> parent,
                                    View v, int position, long id) {

                                String brand = (String) text_sort
                                        .get(position);
                                String size = (String) size_sort
                                        .get(position);
                                String price = (String) price_sort
                                        .get(position);
                                String objID = (String) id_sort
                                        .get(position);
                                String store = (String) store_sort
                                        .get(position);
                                String address = (String) address_sort
                                        .get(position);
                                int percent = (int) percent_sort
                                        .get(position);
                                Integer distance = distance_sort
                                        .get(position);

                                Intent intent = new Intent(
                                        "com.alpha.dealtap.DEALPAGE");
                                intent.putExtra("Brand", brand);
                                intent.putExtra("Size", size);
                                intent.putExtra("Price", price);
                                intent.putExtra("Percent", percent);
                                intent.putExtra("ID", objID);
                                intent.putExtra("Store", store);
                                intent.putExtra("Address", address);
                                intent.putExtra("Distance", distance);

                                System.out.println("My Selection: "
                                        + intent.toString());
                                startActivity(intent);
                            }
                        });

                    }
                }
            }

        }
    });

    list = (Button) findViewById(R.id.sort);
    Button map = (Button) findViewById(R.id.map);
    filter = (Button) findViewById(R.id.filter);

    list.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            PopupMenu popup = new PopupMenu(Search.this, list);
            popup.getMenuInflater().inflate(R.menu.sortmenu,
                    popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getOrder() == 1) {
                        brand.clear();
                        size.clear();
                        price.clear();
                        percent.clear();
                        objID.clear();
                        store.clear();
                        address.clear();
                        distance.clear();
                        listview.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        getDealsPrice();

                        System.out.println(price);

                    } else if (item.getOrder() == 2) {
                        brand.clear();
                        size.clear();
                        price.clear();
                        percent.clear();
                        objID.clear();
                        store.clear();
                        address.clear();
                        distance.clear();


                        listview.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        getDealsPercent();
                        System.out.println(price);

                    } else if (item.getOrder() == 3) {
                        brand.clear();
                        size.clear();
                        price.clear();
                        percent.clear();
                        address.clear();
                        distance.clear();


                        objID.clear();
                        store.clear();
                        listview.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        getDealsName();
                        System.out.println(brand);
                    } else {
                        System.out.println("Shit....Item is: "
                                + item.getTitle());
                    }
                    Toast.makeText(Search.this,
                            "You Clicked : " + item.getTitle(),
                            Toast.LENGTH_SHORT).show();
                    return true;
                }
            });

            popup.show();
        }
    });

    filter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            PopupMenu popupTwo = new PopupMenu(Search.this, filter);
            popupTwo.getMenuInflater().inflate(R.menu.filtermenu,
                    popupTwo.getMenu());
            popupTwo.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getOrder() == 1) {
                        brand.clear();
                        size.clear();
                        price.clear();
                        percent.clear();
                        objID.clear();
                        address.clear();
                        distance.clear();


                        store.clear();
                        listview.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        getDealsName();

                    } else {
                        Intent intent = new Intent(
                                "com.alpha.dealtap.STORESEARCH");
                        startActivity(intent);
                    }
                    return true;
                }
            });

            popupTwo.show();
        }
    });

    map.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.alpha.dealtap.MAP"));

        }

    });

}

private void getDeals() {
    // TODO Auto-generated method stub
    ParseQuery query = new ParseQuery("Deals");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                for (ParseObject dealsObject : objects) {
                    brand.add(dealsObject.getString("Brand"));
                    size.add(dealsObject.getString("Size"));
                    price.add("$" + dealsObject.getString("Price"));
                    percent.add(dealsObject.getInt("Percentage"));
                    objID.add(dealsObject.getObjectId());
                    store.add(dealsObject.getString("Store"));
                    address.add(dealsObject.getString("Address"));

                    Location location = new Location("");
                    location.setLatitude(dealsObject.getInt("Latitude"));
                    location.setLongitude(dealsObject.getInt("Longitude"));

                    distance.add((int) ((userLoc.distanceTo(location))/1609.34));

                }

            } else {
                Log.v("Brand", "Error: " + e.getMessage());
            }
        }
    });

}

private void getDealsPrice() {
    // TODO Auto-generated method stub
    ParseQuery query = new ParseQuery("Deals");
    query.addDescendingOrder("Price");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                for (ParseObject dealsObject : objects) {
                    brand.add(dealsObject.getString("Brand"));
                    size.add(dealsObject.getString("Size"));
                    price.add("$" + dealsObject.getString("Price"));
                    percent.add(dealsObject.getInt("Percentage"));
                    objID.add(dealsObject.getObjectId());
                    store.add(dealsObject.getString("Store"));
                    address.add(dealsObject.getString("Address"));

                    Location location = new Location("");
                    location.setLatitude(dealsObject.getInt("Latitude"));
                    location.setLongitude(dealsObject.getInt("Longitude"));
                    distance.add((int) ((userLoc.distanceTo(location))/1609.34));

                }

            } else {
                Log.v("Brand", "Error: " + e.getMessage());
            }
        }
    });

}

private void getDealsPercent() {
    // TODO Auto-generated method stub
    ParseQuery query = new ParseQuery("Deals");
    query.addDescendingOrder("Percentage");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                for (ParseObject dealsObject : objects) {
                    brand.add(dealsObject.getString("Brand"));
                    size.add(dealsObject.getString("Size"));
                    price.add("$" + dealsObject.getString("Price"));
                    percent.add(dealsObject.getInt("Percentage"));
                    objID.add(dealsObject.getObjectId());
                    store.add(dealsObject.getString("Store"));
                    address.add(dealsObject.getString("Address"));

                    Location location = new Location("");
                    location.setLatitude(dealsObject.getInt("Latitude"));
                    location.setLongitude(dealsObject.getInt("Longitude"));
                    distance.add((int) ((userLoc.distanceTo(location))/1609.34));

                }

            } else {
                Log.v("Brand", "Error: " + e.getMessage());
            }
        }
    });

}

private void getDealsName() {
    // TODO Auto-generated method stub
    ParseQuery query = new ParseQuery("Deals");
    query.addAscendingOrder("Brand");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                for (ParseObject dealsObject : objects) {
                    brand.add(dealsObject.getString("Brand"));
                    size.add(dealsObject.getString("Size"));
                    price.add("$" + dealsObject.getString("Price"));
                    percent.add(dealsObject.getInt("Percentage"));
                    objID.add(dealsObject.getObjectId());
                    store.add(dealsObject.getString("Store"));
                    address.add(dealsObject.getString("Address"));

                    Location location = new Location("");
                    location.setLatitude(dealsObject.getInt("Latitude"));
                    location.setLongitude(dealsObject.getInt("Longitude"));
                    distance.add((int) ((userLoc.distanceTo(location))/1609.34));

                }

            } else {
                Log.v("Brand", "Error: " + e.getMessage());
            }
        }
    });

}

class CustomAdapter extends BaseAdapter {

    String[] data_text;
    String[] size_text;
    String[] price_text;
    String[] id_text;
    String[] store_text;
    String[] address_text;

    Integer[] percent_text;
    Integer[] distance_text;


    CustomAdapter(String[] text, String[] size, String[] price,
            Integer[] percent, String[] id, String[] store,
            String[] address, Integer[] distance) {
        data_text = text;
        size_text = size;
        price_text = price;
        percent_text = percent;
        store_text = store;
        address_text = address;
        id_text = id;
        distance_text = distance;

    }

    CustomAdapter(ArrayList<String> text, ArrayList<String> drinkSize,
            ArrayList<String> price, ArrayList<Integer> percent,
            ArrayList<String> objID, ArrayList<String> store,
            ArrayList<String> address, ArrayList<Integer> distance) {

        data_text = new String[text.size()];
        size_text = new String[drinkSize.size()];
        price_text = new String[price.size()];
        percent_text = new Integer[percent.size()];
        id_text = new String[objID.size()];
        store_text = new String[store.size()];
        address_text = new String[address.size()];
        distance_text = new Integer[distance.size()];


        for (int i = 0; i < text.size(); i++) {
            data_text[i] = text.get(i);
            size_text[i] = drinkSize.get(i);
            price_text[i] = price.get(i);
            percent_text[i] = percent.get(i);
            id_text[i] = objID.get(i);
            store_text[i] = store.get(i);
            address_text[i] = address.get(i);
            distance_text[i] = distance.get(i);


        }

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data_text.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row;

        row = inflater.inflate(R.layout.my_custom_dropdown, parent, false);

        TextView textview = (TextView) row
                .findViewById(R.id.textView_MainStore);
        TextView size = (TextView) row.findViewById(R.id.textView_Size);
        TextView price = (TextView) row.findViewById(R.id.textView_Price);
        TextView percent = (TextView) row
                .findViewById(R.id.textView_dist);
        TextView store = (TextView) row
                .findViewById(R.id.textView_Location);

        textview.setText(data_text[position]);
        size.setText(size_text[position]);
        price.setText(price_text[position]);
        percent.setText(String.valueOf(percent_text[position]) + "%");
        store.setText(store_text[position]);

        // Changes the color of the percent integer based on it's
        // number...green if higher...red if lower
        if (percent_text[position] >= 85)
            percent.setTextColor(getResources().getColor(R.color.green));
        else if (percent_text[position] >= 70
                && percent_text[position] <= 85)
            percent.setTextColor(getResources().getColor(R.color.yellow));
        else if (percent_text[position] >= 0
                && percent_text[position] <= 70)
            percent.setTextColor(getResources().getColor(R.color.red));

        return (row);

    }
}

}

对于大块代码感到抱歉,但我已经坚持了一段时间。 谢谢!

1 个答案:

答案 0 :(得分:0)

你试过吗

adapter = new CustomAdapter(this, R.layout.listview, items);
setListAdapter(adapter);

而不是

listview.setAdapter(adapter);

?在扩展ArrayAdapter时,这对我有用,而不是我只有问题的BaseAdapter。 This tut帮助极大。