Android中使用异步任务自定义自动填充

时间:2015-07-02 09:48:40

标签: android android-asynctask autocomplete

我正在使用AsyncTask实现Custom AutoComplete .AsyncTask用于从服务器获取所需的数据。 我这样做了如下

RequestRecommendations.java文件的内容如下

AutoCompleteTextView request_recommendation_fragment_employee_name;
ArrayList<AutoCompleteItems> ArrayEmployeeName = new  ArrayList<AutoCompleteItems>();
MyAutoCompleteAdapter Adapter_Employee_Name;
AsyncTaskAdvanceSearchRecommendationFragment advanceSearchRecommendationFragment;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    layoutView = inflater.inflate(R.layout.fragment_request_recommendation, container, false);
request_recommendation_fragment_employee_name = (AutoCompleteTextView) layoutView.findViewById(R.id
            .request_recommendation_fragment_employee_name);
    request_recommendation_fragment_employee_name.setThreshold(1);
    Adapter_Employee_Name = new MyAutoCompleteAdapter(getActivity(), ArrayEmployeeName);
 request_recommendation_fragment_employee_name.setAdapter(Adapter_Employee_Name);
 request_recommendation_fragment_employee_name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //   ApplicationStaticMethods.toastDebug(getActivity(),"On text change");

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //    ApplicationStaticMethods.toastDebug(getActivity(),"Before text change");

        }

        @Override
        public void afterTextChanged(Editable s) {
            //

            advanceSearchRecommendationFragment = new    AsyncTaskAdvanceSearchRecommendationFragment();
            advanceSearchRecommendationFragment.execute();
            //  ApplicationStaticMethods.toastDebug(getActivity(),"After text change");


        }
    });


    return layoutView;
}

我的自动完成适配器如下

  public class MyAutoCompleteAdapter extends  ArrayAdapter<AutoCompleteItems> implements Filterable {

    private Activity context;
    private ArrayList<AutoCompleteItems> values;

    public MyAutoCompleteAdapter(Activity context,  ArrayList<AutoCompleteItems> items) {
        super(context, 0, items);
        this.values = items;
        this.context = context;

    }


    public int getCount() {
        return values.size();
    }

//        public Object getItem(int position) {
//            return values.get(position);
//        }

      public long getItemId(int position) {
        return position;
    }


    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        try {

            if (convertView == null) {
                // This a new view we inflate the new layout
                LayoutInflater inflater = (LayoutInflater) context
                        .getApplicationContext().getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
                convertView =  inflater.inflate(R.layout.spinner_item_reg,
                        parent, false);
            }

            AutoCompleteItems spTemp = values.get(position);

            TextView autocompleteItem = (TextView) convertView
                    .findViewById(R.id.regSpinnerTextview);
            convertView.setTag(spTemp.getId());
            autocompleteItem.setText(spTemp.getName().toString());
            autocompleteItem.setBackgroundColor(Color.CYAN);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public Filter getFilter() {
        return null;
    }
}

我的rowitem如下

  public class AutoCompleteItems {

    private String _id;
    private String _name;

    public AutoCompleteItems(String _id, String _name) {
        this._id = _id;
        this._name = _name;
    }

    public String getId() {
        return this._id;
    }

    public void setId(String id) {
        this._id = id;
    }

    public String getName() {
        return this._name;
    }

    public void setName(String name) {
        this._name = name;
    }
}

我的AyncTask如下

  private class AsyncTaskAdvanceSearchRecommendationFragment extends  AsyncTask<String, Void, Void> {

//All operations of getting data
//Data object received in variable responseString

..............................
.............................
..............................

  protected void onPostExecute(Void result) {
        if (progress.isShowing()) {
            progress.dismiss();
        }
        if (!responseString.equals("")) {
            AutoCompleteItems nameObj;

            JSONArray JsonArray;
            try {
                JSONObject Jsonobject1 = new    JSONObject(responseString);
                String status_message = Jsonobject1.getString("status_message");

                if (status_message.equals("Passed")) {
                    String data = Jsonobject1.getString("data");
                    //    ApplicationStaticMethods.toastDebug(getActivity(),data);
                    JsonArray = new JSONArray(data);

                    for (int i = 0; i < JsonArray.length(); i++) {
                        JSONObject Jsonobject = JsonArray.getJSONObject(i);
                        if (webFlag == "empname") {
                            String name = Jsonobject.getString("EMP_NAME");
                            nameObj = new AutoCompleteItems("", name);
                            ArrayEmployeeName.add(nameObj);

                        } 



                    }

                    Adapter_Employee_Name.notifyDataSetChanged();
               }


            } catch (JSONException e) {
                Log.e("Error", "Error: " + e.toString());
            }
        } else {


        }
    }

fragment_request_recommendations.xml的内容

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ScrollView1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">
   <LinearLayout
    android:id="@+id/ll_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ll_code"
    android:orientation="vertical"
    >

     <TextView
          android:id="@+id/request_recommendation_fragment_employee_name_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Employee Name" />

     <aep.tejora.myconnectapp.CustomAutoCompleteTextView
        android:id="@+id/request_recommendation_fragment_employee_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_below="@+id/request_recommendation_fragment_department_name_label"
        android:hint="Type Employee Name"
        android:singleLine="true" />
  </LinearLayout>

 </RelativeLayout>
 </ScrollView>

spinner_item_reg.xml的内容

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

  <TextView
    android:id="@+id/regSpinnerTextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Item"
    android:textColor="#808080"
    android:textSize="16sp" />

  </RelativeLayout>

CustomAutoCompleteTextView.java的内容

  public class CustomAutoCompleteTextView extends AutoCompleteTextView {

  public CustomAutoCompleteTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }

 public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
 }

 public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
 }

// this is how to disable AutoCompleteTextView filter
@Override
  protected void performFiltering(final CharSequence text, final int keyCode) {
    String filterText = "";
    super.performFiltering(filterText, keyCode);
}
}

我的问题是一切似乎工作正常。当我使用调试模式查看变量时,每个变量都按需要设置,我从AsyncTask获取正确的数据。但是当我在自动完成中输入一些值时,它并没有。提出任何建议。 请告诉我我哪里出错了,如果可能的话,请提供我纠正的代码

0 个答案:

没有答案