0

I am trying to populate a spinner with data from mysql/rest service. I want to show only the id's of all employees in spinner.In the drop down box i see id with values and other columns as name: null, age: null etc. But after selecting one, i am able show id value alone by getting id value on my spinner adpater class. How to show id value on drop down box?

@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

        ArrayList<Employee> spinnerArray = new ArrayList<Employee>();
        final SpinnerAdapter spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerArray);
        for (int i = 0; i < response.length(); i++) 
        {
                try {
            spinnerAdapter.add(new Employee(response.getJSONObject(i)));
        } 
    catch (JSONException e) {
        e.printStackTrace();
        }
        }
        spinner.setAdapter(spinnerAdapter);

My adapter class

    public class SpinnerAdapter extends ArrayAdapter<Employee> {

    public SpinnerAdapter(Context context, ArrayList<Employee> employees) {
        super(context, R.layout.spinner_id, employees);
    }

    private static class ViewHolder {
        TextView employeeId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Employee employee = getItem(position);
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.spinner_id, parent, false);

            viewHolder.employeeId = (TextView) convertView.findViewById(R.id.value_employee_employeeId);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.employeeId.setText(employee.getEmployeeId());

        return convertView;

}
user3785322
  • 153
  • 1
  • 6
  • 15

2 Answers2

0

Add your item to your ArrayList which is spinnerArray and not to the spinnerAdapter. Array is where you put your data.

spinnerArray.add(new Employee(response.getJSONObject(i)));

EDIT: to take the Id of each individual data

String TAG = this.getClass().getSimpleName();

    for(Employee employee : spinnerArray){
        Log.d("TAG", employee.getEmployeeId());
    }

I don't know how your Employee.class is like but I hope you get the general idea. Hope this helps you.

mark.jon
  • 107
  • 12
  • I am adding the array response data to the adapter to display. the problem is how i separate the data. – user3785322 Aug 30 '16 at 16:28
  • You first need to add the json data in the ArrayList. then with the line `spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerArray);` this will take the data insidte the `ArrayList spinnerArray ` to your `ArrayAdapter spinnerAdapter`. and then `spinner.setAdapter(spinnerAdapter);` will place it in your list. try executing the `Log.d` in your system to check if the json data is received succesfully. – mark.jon Aug 30 '16 at 16:41
  • I saw you question to the other comment. to convert it to string u can either: `viewHolder.employeeId.setText("" + employee.getEmployeeId());` or `viewHolder.employeeId.setText(String.valueof(employee.getEmployeeId()));` – mark.jon Aug 30 '16 at 18:02
  • I asked if i convert ArrayList to ArrayList how will i get employee details in adapter class. – user3785322 Aug 30 '16 at 18:21
0

You are doing a few things wrong. First of all you populate your array with the json response. If you want to present the ID maybe you should consider using an array of strings ("ID").

ArrayList<String> spinnerArray = new ArrayList<String>();
for (int i = 0; i < response.length(); i++) {
    try {
        spinnerArray.add(new Employee(response.getJSONObject(i)).getID());
    } 
    catch (JSONException e) {
        e.printStackTrace();
    }
}

After that you initialize you adapter

final SpinnerAdapter spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerArray);

And finally you set your adapter to the spinner

spinner.setAdapter(spinnerAdapter);
Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • Please check my adapter class. I ve got array of employee. how do i convert to string. – user3785322 Aug 30 '16 at 17:09
  • i ve got ArrayList in adapter class. you are right i have to change to string to get ID value. if i change to ArrayList how will i get employee details in adapter class. – user3785322 Aug 30 '16 at 18:23
  • I changed to ArrayList in both the classes. Its working now. I think you should change back your answer, so that i can accept your answer :) – user3785322 Aug 31 '16 at 09:29