簡體   English   中英

在列表視圖上添加搜索功能,並使用JSON從Web解析數據

[英]Adding search function on listview with json parse data from web

您好,我是android遠程數據庫的新用戶,並且解析json ..您能幫助我在活動中添加搜索功能嗎?所有我想要的,如果我搜索示例“ 2010-2011學年”,則僅顯示2010-2011年數據。 “ BSBM”部分將出現所有bsbm ..依此類推..謝謝!

我使用此鏈接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();
        }
    }
}

我建議解析數據並將其與模型類映射。 然后,對於json中的對象組,您可以將“學年”與條件匹配,並列出與之匹配的對象的列表,然后將該列表傳遞給適配器。
建議您使用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循環並形成符合條件的對象列表。 將其傳遞給您的適配器。
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
Java的“ for each”循環如何工作?

也請檢查此實現,這可能會對您有所幫助:
無法在Android中遞歸循環通過動態JSON字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM