0

我有这个问题。我需要调用我的 SAX 解析器(每次用户尝试输入学生的姓名)以获取姓名。我现在正在做的是向我的 HttpHandler 发送一个令牌字符串,它每次返回 10 条记录,因为学生总数约为 40K,所以我不能一次全部解析它们。我在 AutoCompletetextview 的 onTextChanged 事件中调用我的 AsyncTask for ParsingXML。但我仍然无法将适配器设置为自动完成。

 public ArrayList<AutoCompleteUserDataGetterSetter> parsexml(String token)
    {
         try {

                SAXParserFactory saxPF = SAXParserFactory.newInstance();



                SAXParser saxP = saxPF.newSAXParser();
                XMLReader xmlR = saxP.getXMLReader();


                URL url = new URL(xyz.com/xyz.ashx?token=+"token"); // URL of the XML


                AutoCompleteUserDataXMLHandler myXMLHandler = new AutoCompleteUserDataXMLHandler();
                xmlR.setContentHandler(myXMLHandler);
                xmlR.parse(new InputSource(url.openStream()));

            } catch (Exception e) {
                System.out.println(e);
            }

            return data=AutoCompleteUserDataXMLHandler.getArrayData();


    }

这是我的 AutoCompleteTextViewAdapter 代码

ArrayList<AutoCompleteUserDataGetterSetter> data=new ArrayList<AutoCompleteUserDataGetterSetter>();

public UserNewMessageAutoCompleteAdapater(Context context,
        int textViewResourceId,
        ArrayList<AutoCompleteUserDataGetterSetter>data) {
    super(context, textViewResourceId, data);
    // TODO Auto-generated constructor stub

       this.context = context;
        this.data=data;


        vi = (LayoutInflater) context
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    final AutoCompleteUserDataGetterSetter i = this.data.get(position);
    if (i != null) {
        Log.d("adapter", "in here");
        AutoCompleteUserDataGetterSetter si = (AutoCompleteUserDataGetterSetter) i;
        v = vi.inflate(R.layout.autocompletetext_layout, null);
        final TextView title = (TextView) v
                .findViewById(R.id.autocomplete_name);
        TextView userid = (TextView) v
        .findViewById(R.id.autocompleteuserid);
        if (title != null)
            title.setText(si.GetFullName());
        userid.setText(si.GetUserId());
    }

    return v;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public AutoCompleteUserDataGetterSetter getItem(int arg0) {
    // TODO Auto-generated method stub
    return data.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

这是我的 AsyncTask 类

class SomeTask extends AsyncTask<Void, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(UserMessagesComposeNewActivity.this);

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... voids) {

        UserMessagesComposeNewActivity.this.data=parsexml(users.getText().toString());

     for(int i=0;i<data.size();i++)
     {
        nameadapter.add(data.get(i).GetFullName());
     useridadapter.add(data.get(i).GetUserId());
     }

if(data==null)
{
   AutoCompleteUserDataGetterSetter umsgs=new AutoCompleteUserDataGetterSetter();
   umsgs.SetFullName(" ");
   umsgs.SetUserID("");
  data.add(umsgs);

}

return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
         if (this.dialog.isShowing()) {



             this.dialog.dismiss();

          }
                users.setAdapter(nameadapter);



    }
}
4

1 回答 1

0

终于经过一番整理,终于把问题解决了!

我放弃了自定义适配器方法并使用了 ArrayAdapter 方法。所以这是我通过选择他们的名字向用户发送消息的完整代码。

public class UserMessagesComposeNewActivity extends Activity implements TextWatcher{

    ArrayList<AutoCompleteUserDataGetterSetter> data=new    
    ArrayList<AutoCompleteUserDataGetterSetter>(); 
    Long login;
    Boolean shouldAutoComplete;
    UserNewMessageAutoCompleteAdapater cdapater;
    String TO,FROM;
    Button btn;
    AutoCompleteTextView users;
    EditText edTextSubject,edTextMessage;
    AutoCompleteWidget myAutoComplete;
    List<String> nameadapter=new ArrayList<String>();
    ArrayAdapter<String> dataAdapter ;
    UserNewMessageAutoCompleteAdapater atAdapter;
    String ResultEmail;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent=getIntent();
        setContentView(R.layout.user_messages_compose_new);
        login=intent.getLongExtra("EmployeedId", 0);

        btn=(Button)findViewById(R.id.userNewmessageSendButton);
        edTextSubject=(EditText)findViewById(R.id.userNewmessageSubject);
        edTextMessage=(EditText)findViewById(R.id.userNewmessageMessage);
        users=(AutoCompleteTextView)findViewById(R.id.userNewmessageAutoCT);
        users.addTextChangedListener(this);
        users.setThreshold(4);



            //Here we get the selected value from autocompletetextview dropdown
            users.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
            // TODO Auto-generated method stub

            for(int i=0;i<data.size();i++)
            {
                if(data.get(i).GetFullName().equals(users.getText().toString()))
                {
                    TO=data.get(i).GetUserId();
                }
                else
                {
                    TO="0";
                }
            }

        }
    });


       //here we call the Send Email method.
       btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                new DomParserEmail().execute();

            }
        });





    }

        public void afterTextChanged(Editable s) {



        }


    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(s.length()>=4)
        {
         new SomeTask().execute();

        }


    }


    class SomeTask extends AsyncTask<Void, Void, Void> {

        private final ProgressDialog dialog = new ProgressDialog(UserMessagesComposeNewActivity.this);

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(Void... voids) {

            //Here we are parsing the xml
            UserMessagesComposeNewActivity.this.data=parsexml(users.getText().toString());



    if(data==null)
    {
       AutoCompleteUserDataGetterSetter umsgs=new AutoCompleteUserDataGetterSetter();
       umsgs.SetFullName(" ");
       umsgs.SetUserID("");
      data.add(umsgs);

    }

    return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
             if (this.dialog.isShowing()) {



                 this.dialog.dismiss();

             }

            UserMessagesComposeNewActivity.this.nameadapter.clear();
             for(int i=0;i<data.size();i++)
             {

      UserMessagesComposeNewActivity.this.nameadapter.add(UserMessagesComposeNewActivity.this.data.get(i).GetFullName());

             }

             dataAdapter = new ArrayAdapter<String>(getApplicationContext(),
             android.R.layout.simple_dropdown_item_1line, nameadapter);
             dataAdapter.notifyDataSetChanged();
             users.setAdapter(dataAdapter);
             users.showDropDown();


        }
    }


     public ArrayList<AutoCompleteUserDataGetterSetter> parsexml(String token)
        {
             try {

                    SAXParserFactory saxPF = SAXParserFactory.newInstance();



                    SAXParser saxP = saxPF.newSAXParser();
                    XMLReader xmlR = saxP.getXMLReader();


                    URL url = new URL("blah blah"); // URL of the XML

                    /** 
                     * Create the Handler to handle each of the XML tags. 
                     **/
                    AutoCompleteUserDataXMLHandler myXMLHandler = new AutoCompleteUserDataXMLHandler();
                    xmlR.setContentHandler(myXMLHandler);
                    xmlR.parse(new InputSource(url.openStream()));

                } catch (Exception e) {
                    System.out.println(e);
                }

                return data=AutoCompleteUserDataXMLHandler.getArrayData();


        }


     class DomParserEmail extends AsyncTask<Void, Void, Void> {

            private final ProgressDialog dialog = new ProgressDialog(UserMessagesComposeNewActivity.this);
            @Override
            protected void onPreExecute() {
                  this.dialog.setMessage("Sending your message");
                  this.dialog.setTitle("Please Wait");
                  this.dialog.show();
            }

            @Override
            protected Void doInBackground(Void... voids) {
              // download and parse

                UserMessagesComposeNewActivity.this.ResultEmail=EmailSuccessMessage(UserMessagesComposeNewActivity.this.edTextSubject.getText().toString(),UserMessagesComposeNewActivity.this.edTextMessage.getText().toString(),UserMessagesComposeNewActivity.this.TO,UserMessagesComposeNewActivity.this.login.toString());

                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                 if (this.dialog.isShowing()) {
                     this.dialog.dismiss();
                    Toast toast=Toast.makeText(getApplicationContext(),UserMessagesComposeNewActivity.this.ResultEmail,Toast.LENGTH_LONG );
                    toast.show();
         }
        }
        }

        public String EmailSuccessMessage(String subject,String content,String To, String From) throws FactoryConfigurationError
        {




             try {

                SAXParserFactory saxPF = SAXParserFactory.newInstance();



                SAXParser saxP = saxPF.newSAXParser();
                XMLReader xmlR = saxP.getXMLReader();


                URL url = new URL("abc.com/abc.asahx?blah blah");

                /** 
                 * Create the Handler to handle each of the XML tags. 
                 **/
                UserReplyMessageXMLParsing myXMLHandler = new UserReplyMessageXMLParsing();
                xmlR.setContentHandler(myXMLHandler);
                xmlR.parse(new InputSource(url.openStream()));

            } 
             catch (Exception e)
             {
                System.out.println(e);
                UserMessagesComposeNewActivity.this.ResultEmail="Email Not Sent";
            }

            UserReplyMessageXMLParsing usmx=new UserReplyMessageXMLParsing();
            UserMessagesComposeNewActivity.this.ResultEmail=usmx.GetEmailStatus();

            return UserMessagesComposeNewActivity.this.ResultEmail;

        }
}
于 2012-09-04T07:58:40.923 回答