amitshekhariitbhu / Fast-Android-Networking

🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀
https://outcomeschool.com
Apache License 2.0
5.71k stars 960 forks source link

Retrieve data and insert into a spinner #265

Open Ahmaadhia opened 6 years ago

Ahmaadhia commented 6 years ago

I am beginner in FAN. Currently I am trying to retrieve data from database using and insert it into spinner. May I know how to do that?

gowtham4us commented 6 years ago

Hi Ahmaadhia.. let me help you. First split your requirements into steps.

  1. Where is the data ? In server ?
  2. If in server, do u have API to access it ?
  3. Using FAN, can you get the data from API ?

With this, FAN's work is over.

  1. Can u store that data into DB ?
  2. Can u get that data from DB ?
  3. Now, can you set that data into spinner ?
Ahmaadhia commented 6 years ago

Hi, thank you for reply my question.

The data is in server and I have created the API. I had tested it in Postman and it works. My problem is to store the data into a spinner.

The API :

$db->query("SELECT visitor_id,name,ic_number,phone,car_plateno,in_time,out_time FROM visitors_info"/,array( "visitor_id"=>$visitorID, "name"=>$visitorName, "ic_number"=>$visitorIC, "phone"=>$visitorPhoneNo, "car_plateno"=>$visitorCarNo, "in_time"=>$time_in, "out_time"=>$time_out )/); while ($row = $db->fetch_assoc()) { $data["log"][]=$row; } echo json_encode($data);

in Android:

ArrayAdapter adapter;

String item2;

private Spinner mySpinner;

mySpinner = (Spinner)view.findViewById(R.id.spinner); display_data = (TextView)view.findViewById(R.id.display_result); //set adapter to spinner Dexter.withActivity(getActivity()) .withPermission(Manifest.permission.INTERNET) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) {

            AndroidNetworking.get(GlobalInfo.TARGET_SERVER_IP)
                    .addPathParameter("action", "resident_lot")
                    .addQueryParameter("lot_dis_id", names)
                    .setTag("")
                    .setPriority(Priority.MEDIUM)
                    .build()

                    .getAsJSONObject(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {

                            try {

                                names = response.getString("lot_dis_id");
                                List<String> item = new ArrayList<>();
                                item.add(names);
                                adapter = new

ArrayAdapter(getContext(), simple_list_item_1, item);

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

                        }

                        @Override
                        public void onError(ANError anError) {
                            anError.printStackTrace();
                            Log.e("Network Error", anError.toString());
                        }
                    });

        }

        @Override
        public void onPermissionDenied(PermissionDeniedResponse response) {

        }

        @Override
        public void

onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {

        }
    }).check();

mySpinner.setAdapter(adapter); mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    item2 = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), "Selected : "+item2,

Toast.LENGTH_SHORT).show();

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

});

On Fri, Apr 20, 2018 at 8:56 PM, gowtham4us notifications@github.com wrote:

Hi Ahmaadhia.. let me help you. First split your requirements into steps.

  1. Where is the data ? In server ?
  2. If in server, do u have API to access it ?
  3. Using FAN, can you get the data from API ?

With this, FAN's work is over.

  1. Can u store that data into DB ?
  2. Can u get that data from DB ?
  3. Now, can you set that data into spinner ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/amitshekhariitbhu/Fast-Android-Networking/issues/265#issuecomment-383086998, or mute the thread https://github.com/notifications/unsubscribe-auth/AY6Sy0YBnQqCMAz1BEzFEARyif23wz8sks5tqdrvgaJpZM4TcxnD .