将搜索功能添加到editText

时间:2015-07-29 06:02:46

标签: java android

我想将搜索功能添加到RelativeLayout(listview_main.xml文件)中的editText。我有listviewadapter设置。我正在获取列表视图。我想添加搜索功能。应将搜索代码添加到mainactivity中。以下是MainActivity.java的代码。我有一个用于搜索的PHP脚本,它返回json数据。

package com.company.napp;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;

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

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


public class MainActivity extends ActionBarActivity {

    // Declare Variables
    ArrayList<HashMap<String, String>> arraylist1;
    int page=1;
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String TITLE = "title";
    static String ADDRESS = "address";
    static String COUNTRY = "country";
    static String IMAGE = "image";
    static String SERVICETYPE="serviceType";
    static String PHNUM="phnum";
    static String DESCRIPTION="description";


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }


    //DownloadJSON AsyncTask

    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Napp");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            //mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page=1");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("Contacts");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    String t,a,d,s;
                    t=jsonobject.getString("title");
                    t=t.replaceAll("\\n", "");
                    //t="\n"+t;
                    a=jsonobject.getString("address");
                    a=a.replaceAll("\\n","");
                    d=jsonobject.getString("description");
                    d=d.replaceAll("\\n","");
                    s=jsonobject.getString("serviceType");
                    s=s.replaceAll("\\n","");
                    //s=s+"\n";

                    map.put("title",t);
                    map.put("address",a);
                    map.put("country", jsonobject.getString("country"));
                    map.put("image", jsonobject.getString("image"));
                    map.put("serviceType",s);
                    map.put("phnum",jsonobject.getString("phnum"));
                    map.put("description",d);
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnScrollListener(new AbsListView.OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                    int threshold = 1;
                    int count = listview.getCount();
                    if (scrollState == SCROLL_STATE_IDLE) {
                        if (listview.getLastVisiblePosition() >= count
                                - threshold) {
                            // Execute LoadMoreDataTask AsyncTask
                            new LoadMoreDataTask().execute();


                        }
                    }

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub

                }

            });

        }

        private class LoadMoreDataTask extends AsyncTask <Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Create a progressdialog
                mProgressDialog = new ProgressDialog(MainActivity.this);
                // Set progressdialog title
                //mProgressDialog.setTitle("Napp");
                // Set progressdialog message
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                // Show progressdialog
               // mProgressDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // Create an array

                page+=1;
                //arraylist = new ArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page="+page);

                try {
                    // Locate the array name in JSON
                    jsonarray = jsonobject.getJSONArray("Contacts");
                    arraylist1 = new ArrayList<HashMap<String, String>>();
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        String t,a,d,s;
                        t=jsonobject.getString("title");
                        t=t.replaceAll("\\n", "");
                        //t="\n"+t;
                        a=jsonobject.getString("address");
                        a=a.replaceAll("\\n","");
                        d=jsonobject.getString("description");
                        d=d.replaceAll("\\n","");
                        s=jsonobject.getString("serviceType");
                        s=s.replaceAll("\\n","");
                        //s=s+"\n";

                        map.put("title",t);
                        map.put("address",a);
                        map.put("country", jsonobject.getString("country"));
                        map.put("image", jsonobject.getString("image"));
                        map.put("serviceType",s);
                        map.put("phnum",jsonobject.getString("phnum"));
                        map.put("description",d);
                        // Set the JSON Objects into the array
                        arraylist1.add(map);

                    }
                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;

            }

            @Override
            protected void onPostExecute(Void args) {

                // Set the JSON Objects into the array
                arraylist.addAll(arraylist1);
                //locate listview last item
                int position =listview.getLastVisiblePosition();
                adapter = new ListViewAdapter(MainActivity.this, arraylist);


                // Set the adapter to the ListView
                listview.setAdapter(adapter);

                //show the lastest retrieved results on the top
                listview.setSelectionFromTop(position,0);


                adapter.notifyDataSetChanged();


                // Close the progressdialog
                mProgressDialog.dismiss();

            }


        }

    }





}

0 个答案:

没有答案