AutoCompleteTextView没有用

时间:2017-03-13 06:25:08

标签: android adapter autocompletetextview

现在我使用autocompleteTextview显示建议,我从webservice获取数据并且我设置了适配器。

我没有收到错误但当我点击Textview时它不会显示任何内容。然后我检查列表并包含数据。

我在这里犯了错误吗?

这是我的代码

 <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/acCustomer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/etCustomerOther"
                android:layout_marginBottom="10dp"
                android:layout_alignLeft="@+id/spCustomer"/>

- 类 -

private static List<String> customerList;

 private class LoadCustomer extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setVisibility(View.VISIBLE);
        pBar.setIndeterminate(false);
        pBar.setClickable(false);
    }
    @Override
    protected Void doInBackground(Void... params) {

        try {
            RESTClient client = new RESTClient(URLService+"get?method=getallcustomerlist&value=");

            client.Execute(RESTClient.RequestMethod.GET);
            responseCustomer = client.getResponse();



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

    @Override
    protected void onPostExecute(Void result) {
        try{

            responseCustomer = responseCustomer.replace("\\\"","\"");
            responseCustomer = responseCustomer.substring(1, responseCustomer.length() - 1);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(responseCustomer);

            String Status = jsonObject.get("Status").toString();

            if (Status == "true"){
                // JSONArray structure = (JSONArray) jsonObject.get("DataList");
                String dataku = jsonObject.get("DataList").toString();

                try {
                    dataku = CRYYPT.decrypt(Enc_Pass, dataku);
                }catch (GeneralSecurityException e){
                    //handle error - could be due to incorrect password or tampered encryptedMsg
                }

                JSONParser parser = new JSONParser();
                JSONArray structure = (JSONArray) parser.parse(dataku);
                customerList = new ArrayList<String>();
                customerList.add("--Choose--");
                for (int i = 0; i < structure.size(); i++) {
                    JSONObject customerlist = (JSONObject) structure.get(i);

                    customerList.add(customerlist.get("CustomerName").toString());
                }
                customerList.add("Other");

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(WorkflowActivity.this, android.R.layout.simple_list_item_1, customerList);
                acCustomer.setAdapter(adapter);

            }else {

            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setClickable(true);
        pBar.setVisibility(View.GONE);
    }

}

- 列出客户项目 -

public class ListCustomerItem {
public String CustName;
/*public String CustLocation;
public String CustID;*/

}

- 列出客户适配器 -

public class ListCustomerAdapter extends ArrayAdapter<ListCustomerItem> {
public ListCustomerAdapter(Context context, List<ListCustomerItem> items)
{
    super(context, R.layout.style_fragment_list_customer, items);
}

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

    if(convertView == null) {
        // inflate the GridView item layout
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.style_fragment_list_customer, parent, false);

        // initialize the view holder
        viewHolder = new ViewHolder();
        viewHolder.tvCustName = (TextView) convertView.findViewById(R.id.tvCustName);
       // viewHolder.tvCustLocation = (TextView) convertView.findViewById(R.id.tvCustLocation);

        convertView.setTag(viewHolder);
    } else {
        // recycle the already inflated view
        viewHolder = (ViewHolder) convertView.getTag();
    }

    // update the item view
    ListCustomerItem item = getItem(position);
    viewHolder.tvCustName.setText(item.CustName);
   // viewHolder.tvCustLocation.setText(item.CustLocation);

    return convertView;
}
private static class ViewHolder {
    TextView tvCustName;
    TextView tvCustLocation;
}
}

2 个答案:

答案 0 :(得分:1)

您是否尝试在AutoCompleteTextView中使用android:completionThreshold="1"

或将mAutoCompleteTextView.setThreshold(1)调用为AutoCompleteTextView引用?

答案 1 :(得分:0)

mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

使用自定义适配器设置AutoCompleteTextView

mAutoCompleteTextView.setSelection(0);
mCustomAutoFilterAdapter = new CustomAutoFilterAdapter(this,R.layout.spinner_row, yourList);
mAutoCompleteTextView.setThreshold(1);
mAutoCompleteTextView.setAdapter(mCustomAutoFilterAdapter);

在自定义适配器类中包含

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(viewResourceId, null);
    }     
    return v;
}

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

Filter nameFilter = new Filter() {
    @Override
    public String convertResultToString(Object resultValue) {
        String str = "get your values from resultValue";
        return str;
    }
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if(constraint != null) {
            suggestions.clear();
              "DO your Activities"
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
       "Do your Activities here"
    }
};