【问题标题】:Android Spinner with POST REQUEST带有 POST REQUEST 的 Android Spinner
【发布时间】:2014-03-04 04:35:19
【问题描述】:

我正在做一些从 web 服务获取数据的项目。现在我要做的是在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的file.xml

<?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" />

我在输出中得到了什么

【问题讨论】:

  • 您是否记录了响应?
  • 记录了响应?你是在说 logcat 吗?
  • 我的意思是看看你是否在teacherNameList中获取数据,可能列表是空的,所以微调器是空的
  • 我得到了一个包含两个元素的数组
  • 为什么你实现了getDropDownViewgetCustomView 方法?它的目的是什么?我认为不需要它,只需getView 方法就足够了。并确保在适配器类中返回数组列表的大小。

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


【解决方案1】:

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

    @Override
public int getCount() {

    return teacherNameList .size();
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多