带有POST REQUEST的Android Spinner

时间:2014-03-04 04:35:19

标签: android android-layout android-spinner custom-adapter

我正在开发一个项目,我可以从webservice获取数据。现在我要做的是在Spinner中显示这些值。我已经尝试但在旋转器中没有任何东西。我已经制作了一个自定义适配器,根据我的要求,一切都很顺利。但我spinner没有得到任何结果。有人可以解决我的问题。在此先感谢。

我是怎么做的:

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teacher_selection);

//  teacherNameList = new ArrayList<HashMap<String, String>>();
    TeacherNameSpinner = (Spinner) findViewById(R.id.spinner);

    teacherNameList = new ArrayList<TeacherNameClass>();
//  TeacherNameSpinner.setOnItemSelectedListener(this);
    new GettingTeacherName().execute();


}

public class GettingTeacherName extends AsyncTask<Void, Void, Void>
{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(GETTING_QUIZ_URL);
         HttpResponse httpResponse = null;
       try {
            httpResponse = httpClient.execute(httpGet);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        HttpEntity httpEntity = httpResponse.getEntity();
    try {
        Serv_Response = EntityUtils.toString(httpEntity);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        try {
            if(Serv_Response != null)
            {
                JSONObject jsonObj = new JSONObject(Serv_Response);
                JSONArray quiz = jsonObj.getJSONArray(TAG_NAME);

                for(int i= 0 ; i < quiz.length(); i++)
                {
                    JSONObject c = quiz.getJSONObject(i);
                    String teacherNames = c.getString(TAG_TEACHERNAME);

//                      HashMap<String, String> hash_teachername = new HashMap<String, String>();
//                      hash_teachername.put(TAG_TEACHERNAME, teacherNames);
//                      
                    TeacherNameClass teacherNameClassObj = new TeacherNameClass();

                    teacherNameClassObj.setTeacherName(teacherNames);
//                      teacherNameList.add(teacherNameClassObj);

                    try {
                        teacherNameList.add(teacherNameClassObj);
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }

                }

            }

        } catch (JSONException e) {
            // TODO: handle exception
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


    //  ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TeacherSelectionActivity.this, android.R.layout.simple_spinner_item);
        CustomTeacherAdapter adapter = new CustomTeacherAdapter(getApplicationContext(),teacherNameList);

        // attaching data adapter to spinner
    //  TeacherNameSpinner.setAdapter(adapter);
        TeacherNameSpinner.setAdapter((SpinnerAdapter) adapter);
    }

}

MainActivity的file.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<!-- Text Label -->
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Category:"
    android:layout_marginBottom="5dp"
/>

<!-- Spinner Element -->
<Spinner 
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/app_name"
/>

CustomAdapterActivity

public class CustomTeacherAdapter extends BaseAdapter{

private Context mContext;
ArrayList<TeacherNameClass> teacherNameList;
//ArrayList<Quiz> quizList;

public CustomTeacherAdapter(Context context,ArrayList<TeacherNameClass> teacherNameList) 
{
      super();
    mContext = context;
    this.teacherNameList = teacherNameList;
}


public View getDropDownView(int position, View convertView,ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}

public View getView(int position, View convertView, ViewGroup parent) 
{
return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) 
{

 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = inflater.inflate(R.layout.activity_custom_teachername_spinner, null);



//    LayoutInflater inflater=getLayoutInflater();
//    View row=inflater.inflate(R.layout.activity_custom_teachername_spinner, parent, false);
TextView label=(TextView)convertView.findViewById(R.id.textView1);
label.setText(teacherNameList.get(position).getTeacherName());

return convertView;
}

CustomAdapter的文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="TextView" />

我在输出中得到了什么

enter image description here

1 个答案:

答案 0 :(得分:0)

只需在您的适配器类中添加getCountgetItem(int pos)方法,如下所示,它返回arraylist和item的大小,它将被夸大到您的微调器中,如下所示:

    @Override
public int getCount() {

    return teacherNameList .size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return teacherNameList.get(position);
}