使用json解析来自web的数据在listview上添加搜索功能

时间:2014-02-03 06:16:40

标签: android json

嗨我在android远程数据库中新建并解析json ..你可以帮我在我的活动上添加搜索功能..如果我搜索示例schoolyear“2010-2011”只有2010-2011数据会出现...或者我搜索“BSBM”部分所有bsbm都会出现..等等..谢谢!

我使用此link 1

但我不知道在哪里放json解析数据..

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议解析数据并将其映射到模型类。然后,对于来自json的对象组,您可以将“schoolyear”与条件匹配,并列出与其匹配的对象,将列表传递给适配器。
建议你使用Gson和模型类:

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/

对于命名差异(根据webservice中的变量),可以使用@SerializedName等注释。 (所以不需要使用Serializable)

在ArrayList中拥有对象后,运行for-each循环并形成符合条件的List列表。将其传递给适配器。
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?

检查这个实现,它可能对你有所帮助:
Unable to Loop through dynamic json string recursively in android