0

我以前在测试时使用 String[] 数组填充我的微调器,现在我已将其更改为使用从 JSON 服务检索列表的 ArrayList,列表显示正常,但是当我选择一个项目时微调器不显示它(这确实当它只是使用一个声明的数组时工作,我所做的只是改变了列表的创建方式,我是一般编程和android的新手,所以如果它很明显的话,对我来说很容易。我真的看过许多其他关于相同的问题发布此之前的主题和教程,但无法发现我的问题。非常感谢任何帮助

    import android.widget.*;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONArray;
    import org.json.JSONObject;

    import java.util.ArrayList;


    public class AddCall extends Activity {

    Spinner customer,jobType;
    Button getSignature;
    //String[] customerList={"Company1","Company2","Company3","Company4"};
    //String[] jobTypeList={"Service","Phone","Installation","Upgrade"};
    ArrayList<String> customerList = new ArrayList<String>();
    ArrayList<String> jobTypeList = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcall);
        getCallTypes();
        getCustomers();

        //setup customer adapter
        customer =(Spinner) findViewById(R.id.spinnerCustomer);
        ArrayAdapter<String> customerAdapter = new ArrayAdapter<String>       

        (AddCall.this,R.layout.spinner_item_text,customerList);
        customer.setAdapter(customerAdapter);

        //setup jobtype adapter
        jobType =(Spinner) findViewById(R.id.spinnerJobType);
        ArrayAdapter<String> jobTypeAdapter = new ArrayAdapter<String>

        (AddCall.this,R.layout.spinner_item_text,jobTypeList);
        jobType.setAdapter(jobTypeAdapter);






        //initialise Signature button
        getSignature =(Button) findViewById(R.id.getSignature);
        getSignature.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(AddCall.this, CaptureSignature.class);
                startActivity(intent);
            }
        });



    }
    public void getCallTypes() {
        DownloadCallTypes taskCallType = new DownloadCallTypes();
        taskCallType.execute(new String[] { "http://192.168.0.14:8080/return_call_type.json" });

    }

    public void getCustomers() {
        DownloadCustomers taskCustomers = new DownloadCustomers();
        taskCustomers.execute(new String[] { "http://192.168.0.14:8080/return_customers.json" });

    }

    private class DownloadCallTypes extends AsyncTask<String, Void, ArrayList<String>> {
        @Override
        protected ArrayList<String> doInBackground(String... urls) {
            ArrayList<String> response = new ArrayList<String>();
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    HttpEntity entity= execute.getEntity();
                    String data = EntityUtils.toString(entity);
                    JSONArray array = new JSONArray(data);

                    for (int i = 0; i < array.length(); i++) {
                        JSONObject row = array.getJSONObject(i);

                        response.add(row.getString("call_type"));
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
            String list="";
            for (int i=0;i< result.size();i++){
                list= result.get(i);
               jobTypeList.add(list);
            }
            //textView.setText(list);
        }
    }

    private class DownloadCustomers extends AsyncTask<String, Void, ArrayList<String>> {
        @Override
        protected ArrayList<String> doInBackground(String... urls) {
            ArrayList<String> response = new ArrayList<String>();
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    HttpEntity entity= execute.getEntity();
                    String data = EntityUtils.toString(entity);
                    JSONArray array = new JSONArray(data);

                    for (int i = 0; i < array.length(); i++) {
                        JSONObject row = array.getJSONObject(i);

                        response.add(row.getString("customer_name"));
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
            String list="";
            for (int i=0;i< result.size();i++){
                list= result.get(i);
                customerList.add(list);
            }
            //textView.setText(list);
        }
    }
   }

这是我的 xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#92add2">
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#fefefe" android:clickable="true" android:baselineAligned="false">
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_gravity="left"
            android:src="@drawable/header_image" android:adjustViewBounds="true"/>
    </LinearLayout>
        <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:paddingTop="10dp">
    <TextView
            style="@style/text"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Customer"
            android:id="@+id/tvCustomer" android:layout_weight="1"/>
    <TextView
            style="@style/text"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Job Type"
            android:id="@+id/tvJobType" android:layout_weight="1"/>
   </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:layout_marginLeft="10dp" android:layout_marginRight="10dp">
    <Spinner
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnerCustomer" android:clickable="true"/>
    <Spinner
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnerJobType" android:clickable="true"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_margin="10dp">
        <TextView
                style="@style/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Address"
                android:id="@+id/tvAddress"/>
        <EditText
                style="@style/editText"
                android:layout_width="fill_parent"
                android:layout_height="120dp"
                android:text=""
                android:id="@+id/etAddress"
                android:background="@drawable/rounded_corners_white"
                />
        <TextView
                style="@style/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phone"
                android:id="@+id/tvPhone"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/etPhone"
                android:background="@drawable/rounded_corners_white" android:textColor="#000000"
                android:paddingLeft="10dp"/>
        <TextView
                style="@style/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Contact"
                android:id="@+id/tvContact"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/etContact"
                android:background="@drawable/rounded_corners_white"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:weightSum="4" android:layout_margin="10dp">
        <TextView
                android:layout_weight="1"
                style="@style/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Logged By"
                android:id="@+id/tvLoggedBy" />
        <EditText
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/etLoggedBy"
                android:background="@drawable/rounded_corners_white"
                style="@style/editText"/>
        <TextView
                android:layout_weight="1"
                style="@style/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Date Logged"
                android:id="@+id/tvDateLogged" android:layout_marginLeft="5dp"/>
        <EditText
                android:background="@drawable/rounded_corners_white"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/editText1"
                style="@style/editText"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_margin="10dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Call Notes"
                android:id="@+id/textView1" style="@style/text"/>
        <EditText
                android:background="@drawable/rounded_corners_white"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:id="@+id/etCallNotes" style="@style/editText"
                android:gravity="left|top"/>
    </LinearLayout>
    <LinearLayout
            android:weightSum="4"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">
        <Button
                style="@style/btnStyleOrange"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save"
                android:id="@+id/bSave" android:layout_gravity="center"       

               android:layout_marginRight="5dp"/>
        <Button
                android:layout_weight="1"
                style="@style/btnStyleOrange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Close"
                android:id="@+id/bClose" android:layout_gravity="center"/>
        <CheckBox
                android:layout_weight="1"
                style="@style/text"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Completed"
                android:id="@+id/checkBox"/>
        <Button
                android:layout_weight="1"
                style="@style/btnStyleOrange"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Signature"
                android:id="@+id/getSignature"/>
    </LinearLayout>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Real Document Management"
            android:id="@+id/textView2" style="@style/text"    

            android:gravity="center_vertical|center_horizontal"
            />
    </LinearLayout>
4

1 回答 1

0

这是因为你Adapter在主线程中设置,whileAsyncTask还没有完成。只需在方法末尾添加以下行onPostExecute

 @Override
        protected void onPostExecute(ArrayList<String> result) {
            String list="";
            for (int i=0;i< result.size();i++){
                list= result.get(i);
               jobTypeList.add(list);
            }
            //textView.setText(list);
jobTypeAdapter.notifyDataSetChanged();
        }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        String list="";
        for (int i=0;i< result.size();i++){
            list= result.get(i);
            customerList.add(list);
        }
        //textView.setText(list);
customerAdapter.notifyDataSetChanged();
    }

基本上每次更改 backing 时都应该这样做ArrayList

注意:您需要将customerAdapter&声明jobTypeAdapter为类级别的变量才能在AsyncTask类中可见。

于 2013-10-20T12:25:54.757 回答