需要帮助根据我的搜索关键字过滤列表数据

时间:2016-07-29 13:03:00

标签: android listview

您好我需要一些帮助来根据我的关键字过滤我的列表。这是我为此编写的代码,但它不起作用。请帮我解决这个问题。

    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;

    /**
     * Created by jignesh on 28-07-2016.
     */
    public class stock_product extends AppCompatActivity {

        String type;
        private static final String LOGIN_URL = "http://www.hetinfosolution.com/AdMobileApp/stock_full_list.php";
        String myJSON;
        private static final String TAG_ID="id";
        private static final String TAG_NAME="name";
        private static final String PTAG_NAME="stock_name";
        private static final String TAG_RESULT="result";
        private static final String SIZE_METER="size_meter";
        private static final String SIZE_SQARE_FEET="size_square_feet";
        private static final String EXP_QTY="expected_qty";
        private static final String EXP_DATE="expected_date";

        JSONArray STOCK_LIST = null;
        String [] items;
        ArrayList<HashMap<String, String>> personList;
        ListView list;
        EditText editText;
        ArrayList<String> listItems;

        // List view
        private ListView lv;

        // Listview Adapter
        ArrayAdapter<String> adapter;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display_stock);
            Intent i = getIntent();
            type = i.getStringExtra("type");

            TextView heading = (TextView) findViewById(R.id.textView);
            heading.setText(type);
            list = (ListView)findViewById(R.id.StockType);
            editText = (EditText)findViewById(R.id.txtSearch);
            personList = new ArrayList<HashMap<String,String>>();

//// LoadData for fill listview data 

            LodaData();


/// here is method for search 

            editText.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) {
                    if(s.toString().equals("")){
                        //Reset ListView 
                        LodaData();
                    }else{


/// SearchItem method created for search string and populate listview after search 


                        SearchItem(s.toString());

                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

        }

    public void SearchItem(String item){

// here write code for search     

    }

    }

        public void LodaData() {
            class stock extends AsyncTask<String, Void, String> {
                ProgressDialog loading;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    loading = ProgressDialog.show(stock_product.this, "Please Wait...", null, true, true);
                }

                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    loading.dismiss();
                    myJSON=s;
                    listView();
                }

                @Override
                protected String doInBackground(String... params) {
                    HashMap<String, String> data = new HashMap<>();
                    data.put("username", type);

                    RegisterUserClass ruc = new RegisterUserClass();
                    String result = ruc.sendPostRequest(LOGIN_URL, data);
                    return result;
                }
            }
            stock ulc = new stock();
            ulc.execute(type);

        }

        private void listView() {
            try {
    //            JSONObject jsonObject = new JSONObject(myJSON);
                JSONObject jsonObject = new JSONObject(myJSON);
                //     jsonObject = new JSONObject(myJSON);
                STOCK_LIST = jsonObject.getJSONArray(TAG_RESULT);

                for(int i=0; i<STOCK_LIST.length(); i++){
                    JSONObject c = STOCK_LIST.getJSONObject(i);
                    String name = c.getString(PTAG_NAME);
                    String size_meter = c.getString(SIZE_METER);
                    String size_square_feet = c.getString(SIZE_SQARE_FEET);
                    String expected_date = c.getString(EXP_DATE);
                    String expected_qty = c.getString(EXP_QTY);

                    HashMap<String,String> STOCK = new HashMap<String, String>();
                    STOCK.put(PTAG_NAME,name);
                    STOCK.put(SIZE_METER,size_meter);
                    STOCK.put(SIZE_SQARE_FEET,size_square_feet);
                    STOCK.put(EXP_DATE,expected_date);
                    STOCK.put(EXP_QTY,expected_qty);

                    personList.add(STOCK);
                }
                ListAdapter adapter = new SimpleAdapter(
                        stock_product.this,personList,R.layout.list_stock,
                        new String[]{PTAG_NAME,SIZE_METER,SIZE_SQARE_FEET,EXP_DATE,EXP_QTY},
                        new int[]{R.id.name,R.id.size_meter,R.id.size_square_feet,R.id.expected_date,R.id.expected_qty}
                );
                list.setAdapter(adapter);
                listItems = new ArrayList(Arrays.asList(personList));

                list.setOnItemClickListener(
                        new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                //String value = (String)list.getItemAtPosition((int)id);
                                TextView c = (TextView)view.findViewById(R.id.name);
                                String value = c.getText().toString();

                                TextView sizeMeter = (TextView)view.findViewById(R.id.size_meter);
                                String valuesizeMeter = sizeMeter.getText().toString();

                                TextView size_square_feet = (TextView)view.findViewById(R.id.size_square_feet);
                                String valuesize_square_feet = size_square_feet.getText().toString();

                                TextView expected_qty = (TextView)view.findViewById(R.id.expected_qty);
                                String valueexpected_qtyet = expected_qty.getText().toString();

                                TextView expected_date = (TextView)view.findViewById(R.id.expected_date);
                                String valueexpected_date = expected_date.getText().toString();
                                double SUM;

                                if(type=="Sonatine Blinds"){
                                    SUM = Float.parseFloat(valuesizeMeter)*Float.parseFloat(valuesize_square_feet)*10.76/2;
                                }else if(type=="Vertical Blinds"){
                                    SUM = Float.parseFloat(valuesizeMeter);
                                }else{
                                    SUM= Float.parseFloat(valuesizeMeter)*Float.parseFloat(valuesize_square_feet)*10.76;
                                }
                                AlertDialog.Builder a_builder = new AlertDialog.Builder(stock_product.this);
                                a_builder.setMessage("PRODUCT NAME  : "+value+"\n"+
                                                    "QUANTITY IN SQF : "+SUM+"\n"+
                                                    "QUANTITY IN METER : "+valuesizeMeter+"\n"+
                                                    "EXPECTED DATE OF ARRIVAL : "+valueexpected_date+"\n"+
                                                    "QUANTITY IN TRANSIT (In Mtr): "+valueexpected_qtyet)
                                 .setCancelable(true)
                                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                     @Override
                                     public void onClick(DialogInterface dialog, int which) {
                                         dialog.cancel();
                                     }
                                 });
                            AlertDialog alert = a_builder.create();
                            alert.setTitle("PRODUCT STOCK DETAIL ");
                            alert.show();
                            }
                        }
                );
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

潜水员是正确的。 在editdetext侦听器的OnTextChanged方法中使用stock_product.this.getFilter()。filter(s)