2

最終的に、Webサービスの結果からオートコンプリートリストを作成しようとしています。これまでのところ、このコードがあります。サービスから返された正しい結果をログに記録しましたが、結果はドロップダウンに入力されないため、選択できます。おそらくアダプターの配置方法に関係していると思います。

 AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMain.this);
                LayoutInflater inflater = SettingsMain.this.getLayoutInflater(); 
                final View searchlayout = inflater.inflate(R.layout.search_friend, null);
               
                friend_name = (AutoCompleteTextView) searchlayout.findViewById(R.id.friend_name);
                friend_name.setThreshold(3);
                dpy = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,adapterList);
                friend_name.setAdapter(dpy);
                friend_name.addTextChangedListener(new TextWatcher() {
                    public void afterTextChanged(Editable editable) {

                    }

                    public void beforeTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {

                    }

                    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                        String text = charSequence.toString();
                        if (text.length() > 3) {
                            new MyAsyncTask().execute(url+text);
                           
                            
                        }
                    }
                });
                
               
                
                adb.setView(searchlayout)
               
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {  
                        
                        
                        String name = friend_name.getText().toString();
                        Log.d( "ECHO" , "text : " + name);
                        return;                  
                       }  
                  })
               .setNegativeButton("Done", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        
                        
                        dialog.cancel(); 
                    }
                }); 
    
                    adb.show();     

これが私の非同期タスククラスです

        class MyAsyncTask extends AsyncTask<String, Void, JSONObject>{
    ArrayList<String> names;
    JSONObject jArray;
    
    @Override
    protected void onPreExecute(){
      
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {
            String url = params[0];
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = null;
            InputStream is = null;
            String result = null;
            StringBuilder sb = null;
            
            
            response = httpClient.execute(new HttpPost(url));
            is = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
            jArray = new JSONObject(result);
                       
           
        } catch(Exception e){
            Log.v("DOIB",e.getMessage());
        }
        return jArray;
    }

    @Override
    protected void onPostExecute(JSONObject result_data){
        try{
            
            for (int i = 0; i < result_data.length(); i++) {
                
                adapterList.add(result_data.getJSONObject("friend"+i).optString("user"));
                Log.d("NMES", result_data.getJSONObject("friend"+i).optString("user").toString());
            }
            dpy.notifyDataSetChanged();
        }catch(Exception e){ Log.d("SYNCERR", e.toString());}
    }

}

ヒントやヘルプをいただければ幸いです。ありがとう :)

更新しました

4

1 に答える 1

1

試みている方法で非同期タスクからデータを返すことはできません。あなたはこのようにそれをしなければなりません:

ArrayList<String> adapterList = new ArrayList<String>();
new MyAsyncTask() {
    @Override
    protected void onPostExecute( ArrayList<String> list ) {
        adapterList = list;
    }
}.execute( ... );
于 2013-01-20T20:19:50.363 に答える