1

i want to have a search bar that searches a number that has been typed in (for example: 115048) and put that in a listview. the json file looks like this http://api.ccapp.it/v1/student/115048/schedule/11 hope someone can help me, the code that i use right now to search a link is like this but it doesnt have a search bar:

public class RoosterviewMd extends ListActivity {

    Button mButton;
    EditText mEdit;

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    //private static String id = null;
    //private static String url = "http://api.ccapp.it/v1/student/" + id + "/schedule/11";
    private static String url = "http://api.ccapp.it/v1/student/115048/schedule/12";

    // JSON Node names
    private static final String TAG_LESSON = "class";
    private static final String TAG_ROOM = "room";
    private static final String TAG_TEACHER = "teacher";

    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.roosterviewmd);

        //Number input
        final EditText input = (EditText) findViewById(R.id.editText2);



        //buttons for all the days

        Button btn2 = (Button) findViewById(R.id.button29);
        btn2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(), "Je ziet je rooster voor maandag al" , Toast.LENGTH_SHORT ).show();

            }
        });

        Button btnOne = (Button)findViewById(R.id.button30);

        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), RoosterviewDi.class);
                startActivity(intent);
            }
        });

        Button btnTwo = (Button)findViewById(R.id.button31);

        btnTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), RoosterviewWo.class);
                startActivity(intent);
            }
        });

        Button btnThree = (Button)findViewById(R.id.button32);

        btnThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), RoosterviewDo.class);
                startActivity(intent);
            }
        });

        Button btnFour = (Button)findViewById(R.id.button33);

        btnFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), RoosterviewVr.class);
                startActivity(intent);
            }
        });
        //Buttons end here

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

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String lesson = ((TextView) view.findViewById(R.id.lesson))
                        .getText().toString();
                String teacher = ((TextView) view.findViewById(R.id.teacher))
                        .getText().toString();
                String room = ((TextView) view.findViewById(R.id.room))
                        .getText().toString();



            }
        });

        // Calling async task to get json
        new GetContacts().execute();

    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(RoosterviewMd.this);
            pDialog.setMessage("Give me a second please");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    JSONArray arr1 = jsonObj.getJSONArray("lessons");


                    JSONArray arr2 = arr1.getJSONArray(0); //Dag

                    for (int b = 0; b < arr2.length(); b++) {
                        JSONObject c = arr2.getJSONObject(b);

                        String lesson = c.getString(TAG_LESSON);
                        String teacher = c.getString(TAG_TEACHER);
                        String room = c.getString(TAG_ROOM);



                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_LESSON, lesson);
                        contact.put(TAG_TEACHER, teacher);
                        contact.put(TAG_ROOM, room);

                        // adding contact to contact list
                        contactList.add(contact);
                    }



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("CCApp", "Couldn't get any data from the url");
                Toast.makeText(getBaseContext(),"We are aware of this error and are working on it, in the mean time eat a cookie", Toast.LENGTH_LONG).show();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(RoosterviewMd.this, contactList,
    R.layout.list_item, new String[] {TAG_LESSON,  TAG_TEACHER,
    TAG_ROOM }, new int[] { R.id.lesson,
    R.id.teacher, R.id.room });





            setListAdapter(adapter);
        }



    }

}

i hope someone can help me with this

Rik
  • 511
  • 5
  • 16
  • I'm not sure if this is the best way, but were you planning on pulling the contents of the JSON webpage to your application, with Gson or something like that? You could pull the Json array into an object or list of objects and search through that. – Benjamin S Apr 08 '15 at 13:56
  • i just want the user tobbe able to use a edit text to search their student number nothing else so it doesnt have to be fancy at all – Rik Apr 08 '15 at 14:03

1 Answers1

0

Check out this answer: Get text from web page to string

Basically, you can simply get the text from the page and pass it into a string, and search the string application side for the contents of your edit text.

If you're looking for more functionality with the data from the web site, I would pull the Json into an array of Jsonobjects using something like Gson. You'd then be able to use the data from the web page in a bit more of a structured manner.

Edit: Now to actually answer your question.

You can include an edit text and button in your xml in order to search using a basic search bar kinda thing.

To set a listener on the button, you would do something like:

findViewById(R.id.button).setOnClickListener(new OnClickListener(){
  @Override
  protected void onClick(View v){
    //Here, we can control what the response to the button press is, and grab the text in the edit text field.
    String editTextString = findViewById(R.id.edittext).getEditableText().toString();
    //Now we have a string used to parse the json or whatever else you need to do. 
    //May want to add a case here if editTextString is null to prevent runtime errors.
  }
}

(Forgive me if there's any minor syntatic errors, just wrote that up quick here in the browser, no API to check on it. :))

Community
  • 1
  • 1
Benjamin S
  • 575
  • 1
  • 7
  • 21
  • they both dont work, do you have any other sollution? – Rik Apr 08 '15 at 19:51
  • Can you elaborate or post code? What is the issue with these solutions exactly? – Benjamin S Apr 08 '15 at 19:53
  • i am pretty noob at android and i just need a simple tutorial that can tell me how i can request those json files. i have tried the first one but that didnt log anything but just closed down, and the second one is too complicated for me to build on because i only need the search bar working – Rik Apr 08 '15 at 19:56
  • Ah, so you just need a search box? Easiest bet would be an edit text and submit button. When the button gets pressed it grabs the text from the edit text, converts it to a string, and search using that. That what you're looking for? – Benjamin S Apr 08 '15 at 20:08
  • finally someone undestrands my problem – Rik Apr 08 '15 at 20:47
  • Yes please that would be very nice – Rik Apr 08 '15 at 21:15
  • I will look at the code in a second benjamin , i hope it will help – Rik Apr 09 '15 at 14:45
  • i have edited the code to my needs and now i have this: http://pastebin.com/t3M1QqG1 and i have a few error with the getEditableText, these are the errors: cant resolve method 'getEditableText()' – Rik Apr 09 '15 at 18:19
  • Sorry, not get EditableText, it's ((EditText) findViewById(R.id.your_edit_text)).getText().toString(); – Benjamin S Apr 09 '15 at 18:37
  • Inconvertible types; cannot cast 'void' to 'android.widget.button' – Rik Apr 09 '15 at 18:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74948/discussion-between-rik-and-benjamin-s). – Rik Apr 10 '15 at 16:10