简体   繁体   中英

Adding search function on listview with json parse data from web

hi im new in android remote database and parsing json.. can you help me to add search function on my activity.. all i want if i search example schoolyear "2010-2011" only 2010-2011 data will appear.. or i search section "BSBM" all bsbm will apear.. and so on.. thanks!

i use this link 1

but i dont know where to put json parse data..

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PortalEnrol extends ListActivity {


            final Context context = this;

        public void alert(){
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Confirmation!"); //Set Alert dialog title here
            alert.setMessage("Are you sure you want to exit?"); //Message here

            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {



                android.os.Process.killProcess(android.os.Process.myPid());

              }
            });

            alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                // Cancelled.
                  dialog.cancel();
              }
            });
            AlertDialog alertDialog = alert.create();
            alertDialog.show();
        }

         @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {

                    alert();
                    return true;
                }
                return super.onKeyDown(keyCode, event);
            }


    private ProgressDialog pDialog;



    private static final String READ_COMMENTS_URL = "http://192.168.0.102/new/login2.php";




    private static final String TAG_SUCCESS = "success";
    private static final String TAG_subjectTitle = "subjectTitle";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_POST_ID = "post_id";
    private static final String TAG_subjectcode = "subjectcode";
    private static final String TAG_schedcode = "schedcode";
    private static final String TAG_units = "units";

    private static final String TAG_semester = "semester";
    private static final String TAG_schoolyear = "schoolyear";
    private static final String TAG_section = "section";
    private static final String TAG_status = "status";




    private JSONArray mComments = null;

    private ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.enrol);
    }

    @Override
    protected void onResume() {

        super.onResume();

        new LoadComments().execute();
    }



    public void updateJSONdata() {



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


        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);


        try {


            mComments = json.getJSONArray(TAG_POSTS);


            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);


                String subjectTitle = c.getString(TAG_subjectTitle);
                String schedcode = c.getString(TAG_schedcode);
                String subjectcode = c.getString(TAG_subjectcode);
                String units = c.getString(TAG_units);

                String semester = c.getString(TAG_semester);
                String schoolyear = c.getString(TAG_schoolyear);
                String section = c.getString(TAG_section);
                String status = c.getString(TAG_status);



                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_subjectTitle, subjectTitle);
                map.put(TAG_schedcode, schedcode);
                map.put(TAG_subjectcode, subjectcode);
                map.put(TAG_units, units);

                map.put(TAG_semester, semester);
                map.put(TAG_schoolyear, schoolyear);
                map.put(TAG_section, section);
                map.put(TAG_status, status);


                mCommentList.add(map);


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * Inserts the parsed data into the listview.
     */
    private void updateList() {

        ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                R.layout.single_post2, new String[] { TAG_subjectTitle, TAG_schedcode,
                        TAG_subjectcode ,TAG_units, TAG_semester ,TAG_schoolyear, TAG_section ,TAG_status }, new int[] { R.id.subjectTitle, R.id.schedcode,
                        R.id.subjectcode,R.id.units,  R.id.semester, R.id.schoolyear, R.id.section, R.id.status});

        setListAdapter(adapter);


        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {



            }
        });
    }

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(PortalEnrol.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {
            updateJSONdata();
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            updateList();
        }
    }
}

I'd suggest parsing the data and mapping it with model classes. Then for the group of objects from json, you can match the "schoolyear" with the criteria and make a list of the objects that match it, pass the list to the adapter.
Recommend you use Gson and model classes:

Gson gson = new Gson();  
ModelClass modelClass= new ModelClass();  
modelClass= gson.fromJson(responseContent,ModelClass.class);   
//where responseContent is your jsonString  
Log.i("Web service response", ""+modelClass.toString());   

https://code.google.com/p/google-gson/

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName. (So no need to use Serializable)

Once you have the objects in ArrayList, run a for-each loop and form a List of ones that match your criteria. Pass that to your adapter.
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?

Check this implementation too, it may be of some help to you:
Unable to Loop through dynamic json string recursively in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM