搜索过滤器移动到标签片段

时间:2016-12-10 17:17:13

标签: android search tabs

我正在处理一个搜索页面的活动。它包含一些带有edittext和spinner的值。在给出值并单击搜索按钮后,它将移动到选项卡片段,其中包含搜索结果除以三个选项卡(按日期,按价格,按城市)。我刚刚测试了日期,但它没有显示。请帮帮我。

搜索页面:

public class Search extends AppCompatActivity {    
    EditText name,to,from;
    Spinner cscope,strainer,sinstitute,scity,scountry,cstype,sgender,sdisable,sprice;
    String[] able={"Yes","No"};
    String[] sex={"Male only","Female only","Both Male and Female"};
    String[] price={"Free","1900S.R","1500S.R"};
    String[] city={"Riyadh"};
    Button search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        name= (EditText) findViewById(R.id.search_csname);
        cscope= (Spinner) findViewById(R.id.search_scope);
        strainer= (Spinner) findViewById(R.id.search_trainerName);
        sinstitute= (Spinner) findViewById(R.id.search_instName);
        scity= (Spinner) findViewById(R.id.search_city);
        scountry= (Spinner) findViewById(R.id.search_country);
        cstype= (Spinner) findViewById(R.id.search_ctype);
        sgender= (Spinner) findViewById(R.id.search_gender);
        sdisable= (Spinner) findViewById(R.id.search_disabled);
        sprice= (Spinner) findViewById(R.id.search_price);
        search= (Button) findViewById(R.id.searchNow);

        ArrayAdapter<String> gen=new ArrayAdapter<String>(Search.this,android.R.layout.simple_spinner_item,sex);
        gen.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sgender.setAdapter(gen);

        ArrayAdapter<String> disableness=new ArrayAdapter<String>(Search.this,android.R.layout.simple_spinner_item,able);
        disableness.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sdisable.setAdapter(disableness);

        ArrayAdapter<String> pricess=new ArrayAdapter<String>(Search.this,android.R.layout.simple_spinner_item,price);
        pricess.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sprice.setAdapter(pricess);

        ArrayAdapter<String> cities=new ArrayAdapter<String>(Search.this,android.R.layout.simple_spinner_item,city);            cities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        scity.setAdapter(cities);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String sname=name.getText().toString();
                final String seprice=sprice.getSelectedItem().toString();
                final String secity=scity.getSelectedItem().toString();
                final String sedate=from.getText().toString();
                Intent s=new Intent(getApplicationContext(),SearchResults.class);
                s.putExtra("csname",sname);
                s.putExtra("csprice",seprice);
                s.putExtra("cscity",secity);
                s.putExtra("csdate",sedate);
                startActivity(s);
            }
        });    
    }

    public void selectToDate(View view){
        DialogFragment tofrag=new SelectTodateFragment();
        tofrag.show(getSupportFragmentManager(),"Date Picker");
    }
    public void poptoDate(int date,int month,int year){

        to= (EditText) findViewById(R.id.search_to);
        assert to != null;
        to.setText(date+"/"+month+"/"+year);
    }

    @SuppressLint("ValidFragment")
    public class SelectTodateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{    
       public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar tocal=Calendar.getInstance();
            int yy=tocal.get(Calendar.YEAR);
            int mm=tocal.get(Calendar.MONTH);
            int dd=tocal.get(Calendar.DAY_OF_MONTH);
           return new DatePickerDialog(getActivity(), this, yy, mm, dd);

        }
        @Override
        public void onDateSet(DatePicker view, int year, int mm, int dd) {
            poptoDate(year, mm+1, dd);
        }    
    }

    public void selectFromDate(View view){
        DialogFragment newfrag=new SelectFromdateFragment();
        newfrag.show(getSupportFragmentManager(),"Date Picker");
    }

    public void popDate(int date,int month,int year){
        from= (EditText) findViewById(R.id.search_from);
        assert from != null;
        from.setText(date+"/"+month+"/"+year);    
    }

    @SuppressLint("ValidFragment")
    public class SelectFromdateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
        public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);    
        }
        @Override
        public void onDateSet(DatePicker view, int year, int mm, int dd) {
            popDate(year, mm+1, dd);
        }
    }
}

搜索结果页面:

public class SearchResults extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

    private TabLayout tabs;
    private ViewPager viewPager;
    Bundle bundle=new Bundle();
    private SearchPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        tabs = (TabLayout) findViewById(R.id.search_tabLayout);
        viewPager = (ViewPager) findViewById(R.id.search_pager);

        tabs.addTab(tabs.newTab().setText("By Price"));
        tabs.addTab(tabs.newTab().setText("By Date"));
        tabs.addTab(tabs.newTab().setText("By City"));
        tabs.setTabGravity(tabs.GRAVITY_FILL);
        tabs.setHorizontalScrollBarEnabled(false);

        pager = new SearchPager(getSupportFragmentManager(), tabs.getTabCount());
        viewPager.setAdapter(pager);

        Intent h=getIntent();
        String cname=h.getStringExtra("csname");
        String byprice=h.getStringExtra("csprice");
        String bydate=h.getStringExtra("csdate");
        String bycity=h.getStringExtra("cscity");
        bundle.putString("coname",cname);
        bundle.putString("coprice",byprice);
        bundle.putString("codate",bydate);
        bundle.putString("cocity",bycity);
        pager.getData(bundle);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));

        tabs.setOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {    

    }    
}

搜索pagerAdapter:

public class SearchPager extends FragmentStatePagerAdapter {
    int tabCount;
    private Bundle args=new Bundle();

    public SearchPager(FragmentManager fm, int tabCount) {
        super(fm);
        this.tabCount = tabCount;
    }

    @Override
    public Fragment getItem(int position) {   
        switch (position) {
            case 0:
                Fragment tab1 = new SearchPrice();
                getData(args);
                tab1.setArguments(args);
                return tab1;
            case 1:
                Fragment tab2 = new SearchDate();
                getData(args);
                tab2.setArguments(args);
                return tab2;
            case 2:
                Fragment tab3 = new SearchCity();
                getData(args);
                tab3.setArguments(args);
                return tab3;
            default:
                return null;
        }

    }
    @Override
    public int getCount() {
        return tabCount;
    }

    public void getData(Bundle bundle) {
        this.args=bundle;
    }
}

搜索BYPrice:

public class SearchPrice extends Fragment {

    private ListView listView;
    private ProgressDialog mprogress;
    ArrayList<HashMap<String, String>> alist = new ArrayList<>();
    private SpriceAdapter adapter;
    TextView id;
    Handler mHandler;
    static String sname;
    static String sprice;

    public SearchPrice() {
        // Required empty public constructor
    }    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_search_price, container, false);

        mHandler = new Handler();

        id = (TextView) rootView.findViewById(R.id.copriceId);
        listView = (ListView) rootView.findViewById(R.id.searchPriceList);

        adapter = new SpriceAdapter(getActivity(), alist);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setScrollingCacheEnabled(false);

        if (getArguments() != null) {

            sname = this.getArguments().getString("coname");
            sprice = this.getArguments().getString("coprice");
        }
        final String turl = "http://adoxsolutions.in/numuww/services/courses";

        new ByPrice(this).execute(turl);
        return rootView;
    }

    private class ByPrice extends AsyncTask<String, Void, Void> {

        private ProgressDialog dialog;    

        public ByPrice(SearchPrice activity) {
            dialog = new ProgressDialog(activity.getContext());
        }

        @Override
        protected void onPreExecute() {
            dialog.setMessage("Loading Data...");
            dialog.show();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }

        protected Void doInBackground(String... turl) {

            try {

                URL url = new URL(turl[0]);
                HttpURLConnection connect = (HttpURLConnection) url.openConnection();
                connect.setRequestMethod("POST");

                System.out.println("Response Code:" + connect.getResponseCode());
                InputStream in = new BufferedInputStream(connect.getInputStream());
                String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
                System.out.println(response);
                Log.d("VALUE:", response);

                JSONObject obj = new JSONObject(response);
                JSONArray jsArray = obj.optJSONArray("Course");
                for (int k = 0; k < jsArray.length(); k++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject jobj = jsArray.getJSONObject(k);
                    final String cid = jobj.getString("id");
                    final String cn = jobj.getString("course");
                    final String dt = jobj.getString("date");
                    map.put("id", cid);
                    map.put("course", cn);
                    map.put("date", dt);
                    map.put("trainer", jobj.getString("trainer"));
                    map.put("country", jobj.getString("country"));
                    map.put("city", jobj.getString("city"));
                    map.put("days", jobj.getString("no_days"));
                    map.put("hours", jobj.getString("tot_hrs"));
                    map.put("img", jobj.getString("img"));
                    alist.add(map);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (cn.equals(sname) || dt.equals(sprice)) {
                                Log.d("Value", cid);
                                id.setText(cid);
                                listView.setAdapter(adapter);
                            }
                        }
                    });
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }    
    }

    class SpriceAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
        private Typeface typeface;

        public SpriceAdapter(Context c, ArrayList<HashMap<String, String>> list) {
            context = c;
            MyArr = list;
        }

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

        @Override
        public Object getItem(int position) {
            return position;
        }

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

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final String id = MyArr.get(position).get("id");

            if (convertView != null) {
                convertView = inflater.inflate(R.layout.searchlist, parent, false);
                TextView cname = (TextView) convertView.findViewById(R.id.searchlistName);
                TextView tname = (TextView) convertView.findViewById(R.id.strainerName);
                TextView country = (TextView) convertView.findViewById(R.id.searchcoName);
                TextView city = (TextView) convertView.findViewById(R.id.searchciName);
                TextView date = (TextView) convertView.findViewById(R.id.sdateTime);
                TextView time = (TextView) convertView.findViewById(R.id.scourseTime);
                TextView hours = (TextView) convertView.findViewById(R.id.scourseHours);
                ImageView image = (ImageView) convertView.findViewById(R.id.scphoto);
                String cn = (MyArr.get(position).get("course"));
                String tn = (MyArr.get(position).get("trainer"));
                String co = (MyArr.get(position).get("country"));
                String ci = (MyArr.get(position).get("city"));
                String dat = (MyArr.get(position).get("date"));
                String dys = (MyArr.get(position).get("days"));
                String hrs = (MyArr.get(position).get("hours"));
                String c = context.getString(R.string.comma);
                String ob = " ( ";
                String cb = " ) ";
                String h = " Hours";
                String d = " Days";
                String trainerName = tn + c;
                String cdays = dys + d;
                String chrs = ob + hrs + h + cb;

                try {
                    if(hrs != null || !hrs.equals("")) {
                        String h1="0 Hours";
                        String chs=ob + h1 + cb;
                        hours.setText(chs);
                    }else{
                        hours.setText(chrs);
                    }

                    //   image.setImageBitmap(loadBitmap(hlist.get(position).get("img")));
                    Glide.with(context).load(MyArr.get(position).get("img")).into(image);

                    cname.setText(cn);
                    tname.setText(trainerName);
                    country.setText(co);
                    city.setText(ci);
                    date.setText(dat);
                    time.setText(cdays);

                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent o=new Intent(context,CourseScreen.class);
                            o.putExtra("id",id);
                            context.startActivity(o);
                        }
                    });
                } catch(Exception e){
                    e.printStackTrace();
                }

            }
            return convertView;
        }    
    }
}

搜索日期:

public class SearchDate extends Fragment {

    private ListView listView;
    private ProgressDialog mprogress;
    TextView id;
    ArrayList<HashMap<String, String>> alist = new ArrayList<>();
    private SdateAdapter adapter;
    Handler mHandler;
    static String sname;
    static String sdate;

    public SearchDate() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_search_date, container, false);

        mHandler = new Handler();

        id= (TextView) rootView.findViewById(R.id.courseId);
        listView= (ListView) rootView.findViewById(R.id.searchDateList);

        adapter = new SdateAdapter(getActivity(), alist);
        listView.setAdapter(adapter);
        listView.setScrollingCacheEnabled(false);
        adapter.notifyDataSetChanged();    

        if (getArguments() != null) {

            sname = this.getArguments().getString("coname");
            sdate = this.getArguments().getString("codate");
        }
        final String turl = "http://adoxsolutions.in/numuww/services/courses";

        new ByDate(this).execute(turl);
        return rootView;
    }

    private class ByDate extends AsyncTask<String, Void, Void> {

        private ProgressDialog dialog;


        public ByDate(SearchDate activity)
        {
            dialog = new ProgressDialog(activity.getContext());
        }

        @Override
        protected void onPreExecute() {
            dialog.setMessage("Loading Data...");
            dialog.show();
        }

        protected Void doInBackground(String... turl) {
            try {

                URL url = new URL(turl[0]);
                HttpURLConnection connect = (HttpURLConnection) url.openConnection();
                connect.setRequestMethod("POST");

                System.out.println("Response Code:" + connect.getResponseCode());
                InputStream in = new BufferedInputStream(connect.getInputStream());
                String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
                System.out.println(response);
                Log.d("VALUE:", response);

                JSONObject obj = new JSONObject(response);
                JSONArray jsArray = obj.optJSONArray("Course");
                for (int k = 0; k < jsArray.length(); k++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject jobj = jsArray.getJSONObject(k);
                    final String cid=jobj.getString("id");
                    final String cn=jobj.getString("course");
                    final String dt=jobj.getString("date");
                    map.put("id",cid);
                    map.put("course",cn);
                    map.put("date", dt);
                    map.put("trainer", jobj.getString("trainer"));
                    map.put("country", jobj.getString("country"));
                    map.put("city", jobj.getString("city"));
                    map.put("days", jobj.getString("no_days"));
                    map.put("hours", jobj.getString("tot_hrs"));
                    map.put("img", jobj.getString("img"));
                    alist.add(map);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(cn.equals(sname) || dt.equals(sdate) ) {
                                Log.d("Value",cid);
                                id.setText(cid);    
                                }
                        }
                    });

                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }   

    class SdateAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
        private Typeface typeface;

        public SdateAdapter(Context c, ArrayList<HashMap<String, String>> list) {
            context = c;
            MyArr = list;
        }

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

        @Override
        public Object getItem(int position) {
            return position;
        }

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

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final String id = MyArr.get(position).get("id");
            final String dt=MyArr.get(position).get("date");

            if (convertView != null) {
                convertView = inflater.inflate(R.layout.searchlist, parent, false);
                TextView cname = (TextView) convertView.findViewById(R.id.searchlistName);
                TextView tname = (TextView) convertView.findViewById(R.id.strainerName);
                TextView country = (TextView) convertView.findViewById(R.id.searchcoName);
                TextView city = (TextView) convertView.findViewById(R.id.searchciName);
                TextView date = (TextView) convertView.findViewById(R.id.sdateTime);
                TextView time = (TextView) convertView.findViewById(R.id.scourseTime);
                TextView hours = (TextView) convertView.findViewById(R.id.scourseHours);
                ImageView image = (ImageView) convertView.findViewById(R.id.scphoto);
                String cn = (MyArr.get(position).get("course"));
                String tn = (MyArr.get(position).get("trainer"));
                String co = (MyArr.get(position).get("country"));
                String ci = (MyArr.get(position).get("city"));
                String dat = (MyArr.get(position).get("date"));
                String dys = (MyArr.get(position).get("days"));
                String hrs = (MyArr.get(position).get("hours"));
                String c = context.getString(R.string.comma);
                String ob = " ( ";
                String cb = " ) ";
                String h = " Hours";
                String d = " Days";
                String trainerName = tn + c;
                String cdays = dys + d;
                String chrs = ob + hrs + h + cb;

                try {
                    if(hrs != null || !hrs.equals("")) {
                        String h1="0 Hours";
                        String chs=ob + h1 + cb;
                        hours.setText(chs);
                    }else{
                        hours.setText(chrs);
                    }

                    //   image.setImageBitmap(loadBitmap(hlist.get(position).get("img")));
                    Glide.with(context).load(MyArr.get(position).get("img")).into(image);

                    cname.setText(cn);
                    tname.setText(trainerName);
                    country.setText(co);
                    city.setText(ci);
                    date.setText(dat);
                    time.setText(cdays);

                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent o=new Intent(context,CourseScreen.class);
                            o.putExtra("id",id);
                            context.startActivity(o);
                        }
                    });
                } catch(Exception e){
                    e.printStackTrace();
                }

            }
            return convertView;
        }
    }
}

与Search ByCity相同:

0 个答案:

没有答案
相关问题