0

这是我尝试过的,但是当我测试搜索框时没有任何显示。

@SuppressLint("NewApi")  
public class east extends Fragment {

    ListView list;
    TextView id;
    TextView name;
    Button Btngetdata;
    east adaptor;
    EditText Search;

    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();



    //URL to get JSON Array
    private static String url = "http://YYY.com/s/east.php";

    //JSON Node Names
    private static final String TAG_ARRAY = "coffee";
    private static final String TAG_ID = "ID";
    private static final String TAG_NAME = "name";
    private static final String TAG_adress = "adress";
    private static final String TAG_IMAGE = "image";

    JSONArray coffee = null;

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

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        if (Connectivity.isConnected(getActivity())){
            Toast.makeText(getActivity(), "conected", Toast.LENGTH_SHORT);
        }else{
            AlertDialog alertDialog1 = new AlertDialog.Builder(getActivity()).create();

            // Setting Dialog Title
            alertDialog1.setTitle(" error");

            // Setting Dialog Message
            alertDialog1.setMessage("no connection");

            // Setting Icon to Dialog
            alertDialog1.setIcon(R.drawable.east);

            // Setting OK Button
            alertDialog1.setButton("exit", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    // closed
                    System.exit(0);
                }
            });

            // Showing Alert Message
            alertDialog1.show();
    }

        oslist = new ArrayList<HashMap<String, String>>();
        Btngetdata = (Button)getView().findViewById(R.id.getdata);

    Btngetdata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            new JSONParse().execute();  
            Btngetdata.setVisibility(View.GONE);
            }
        });

}

    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;
        JsonParser jParser = new JsonParser();
        private JSONObject json;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            name = (TextView)getView().findViewById(R.id.name);
            id = (TextView)getView().findViewById(R.id.id);

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();


        }

        @Override
        protected JSONObject doInBackground(String... args) {

            // Getting JSON from URL
            json = jParser.getJSONFromUrl(url);

            return json;
        }



        @Override
        protected void onPostExecute(JSONObject json) {

            pDialog.dismiss();
            try {
                 // Getting JSON Array from URL
                 coffee = json.getJSONArray(TAG_ARRAY);

                 for(int i = 0; i < coffee.length(); i++){

                     JSONObject c = coffee.getJSONObject(i);

                     // Storing  JSON item in a Variable
                     String ID = null;
                     String name = null;
                     String adress = null;
                     String image = null;
                     try {
                        ID = new String(c.getString(TAG_ID).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        name = new String(c.getString(TAG_NAME).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        adress = new String(c.getString(TAG_adress).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                            image = new String(c.getString(TAG_IMAGE).getBytes("UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                     // Adding value HashMap key => value
                     HashMap<String, String> map = new HashMap<String, String>();
                     map.put(TAG_ID, ID);
                     map.put(TAG_NAME, name);
                     map.put(TAG_adress, adress);
                     map.put(TAG_IMAGE, image);

                     oslist.add(map);

                     list = (ListView)getView().findViewById(R.id.listView1);

                     ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                         R.layout.listview,
                         new String[] { TAG_ID, TAG_NAME, TAG_adress }, new int[] {
                             R.id.id, R.id.name, R.id.adress});
                     list.setAdapter(adapter);

                Search = (EditText) getView().findViewById (R.id.wsSearch);                 
                list.setTextFilterEnabled(true);
                Search.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)
                    {

                        ArrayList<HashMap<String, String>> arrayTemplist = new ArrayList<HashMap<String, String>>();
                        String searchString = Search.getText().toString();
                        if(searchString.length()>0)
                        {
                        for (int i1 = 1; i1 < oslist.size(); i1++)
                        {
                            String currentString = oslist.get(i1).get(east.TAG_NAME);
                            if (searchString.equalsIgnoreCase(currentString))
                            {
                                arrayTemplist.add(oslist.get(i1));
                            }
                        }
                        ListAdapter adapter = new SimpleAdapter(getActivity(), arrayTemplist,
                                R.layout.listview,
                                new String[] { TAG_ID, TAG_NAME, TAG_adress }, new int[] {
                                    R.id.id, R.id.name, R.id.adress});
                        list.setAdapter(adapter);
                        }
                    else
                    {
                        ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                                R.layout.listview,
                                new String[] { TAG_ID, TAG_NAME, TAG_adress }, new int[] {
                                    R.id.id, R.id.name, R.id.adress});
                        list.setAdapter(adapter);
                    }
                    }
                });
                     list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                        @Override
                         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(getActivity(), SingleItemView.class); 
                                // Pass all data rank
                                intent.putExtra("rank", oslist.get(+position).get("ID"));
                                // Pass all data country
                                intent.putExtra("country", oslist.get(+position).get("name"));
                                // Pass all data population
                                intent.putExtra("population", oslist.get(+position).get("adress"));
                                // Pass all data flag
                                intent.putExtra("flag", oslist.get(+position).get("image"));
                                // Start SingleItemView Class
                                startActivity(intent);
                         }
                     });
                     }
            } catch (JSONException e) {
              e.printStackTrace();
            }
        }
    }
}

Logcat 是空的,什么也不显示。我无法追踪搜索过程到底出了什么问题。我提前感谢您的帮助。

4

0 回答 0