Android 为什么从服务返回时ListView不更新?

Android 为什么从服务返回时ListView不更新?,android,android-studio,android-fragments,android-activity,android-service,Android,Android Studio,Android Fragments,Android Activity,Android Service,选择你的国家 import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; public class Choose_Country extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState)

选择你的国家

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class Choose_Country extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose__country);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}
选择_CountryFragment

package com.A.B;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.A.B.service.GetNumberService;

/**
 * A placeholder fragment containing a simple view.
 */
public class Choose_CountryFragment extends Fragment {

    private RadioGroup radioGroup;

    public Choose_CountryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_choose__country, container, false);

        radioGroup = (RadioGroup) rootView.findViewById(R.id.country_choice_radio);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.A:
                        // do operations specific to this selection
                        break;

                    case R.id.B:
                        // do operations specific to this selection
                        Intent explicitGetNumberServiceIntent = new Intent(getActivity(), GetNumberService.class);
                        explicitGetNumberServiceIntent.putExtra("country", "B");
                        getActivity().startService(explicitGetNumberServiceIntent);
                        break;
                    case R.id.C:
                        Intent explicitGetNumberServiceIntentN = new Intent(getActivity(), GetNumberService.class);
                        explicitGetNumberServiceIntentC.putExtra("country", "C");
                        getActivity().startService(explicitGetNumberServiceIntentC);
                        break;
                }
            }
        });
        return rootView;
    }


}
GetNumberService

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * helper methods.
 *
 *
 */
public class GetNumberService extends IntentService {
    private ArrayList<String> mobileNumberList = new ArrayList<String>();
    public static final String ABC = "com.A.B.ABC";
    private final String LOG_TAG = GetNumberService.class.getSimpleName();

    public GetNumberService() {
        super("GetNumberService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;


        String NumbersJSON = null;


        if (intent != null) {
            String chosenCountry = intent.getStringExtra("country");

            try{

                final String BASE_URL = "http://1.1.1.1:8080/WebServices/country/";

                Uri buildUri = Uri.parse(BASE_URL).buildUpon()
                        .appendPath(chosenCountry).build();

                URL url = new URL(buildUri.toString());
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    // Nothing to do.
                    return;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                    // But it does make debugging a *lot* easier if you print out the completed
                    // buffer for debugging.
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return;
                }

                NumbersJSON = buffer.toString();

                JSONArray Numbers_Array = new JSONArray(NumbersJSON);

                for(int i = 0; Numbers_Array.length() != 0 && i < Numbers_Array.length(); i++) {
                    NumberList.add(Numbers_Array.getString(i));
                }

                Intent NumbersIntent = new Intent(ABC);
                NumbersIntent.putStringArrayListExtra("Numbers", NumberList);

                LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
                localBroadcastManager.sendBroadcast(NumbersIntent);

            } catch (IOException e) {
                Log.e(LOG_TAG, "Error ", e);
                // If the code didn't successfully get the weather data, there's no point in attempting
                // to parse it.
            } catch (JSONException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }
            }
        }
        return;
    }


}
导入android.app.IntentService;
导入android.content.Intent;
导入android.net.Uri;
导入android.support.v4.content.LocalBroadcastManager;
导入android.util.Log;
导入org.json.JSONArray;
导入org.json.JSONException;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.ArrayList;
/**
*用于在中处理异步任务请求的{@link IntentService}子类
*独立处理程序线程上的服务。
*助手方法。
*
*
*/
公共类GetNumberService扩展了IntentService{
private ArrayList mobileNumberList=new ArrayList();
公共静态最终字符串ABC=“com.A.B.ABC”;
私有最终字符串LOG_TAG=GetNumberService.class.getSimpleName();
公共GetNumberService(){
超级(“GetNumberService”);
}
@凌驾
受保护的手部内容无效(意图){
HttpURLConnection-urlConnection=null;
BufferedReader reader=null;
字符串NumbersJSON=null;
if(intent!=null){
String chosenCountry=intent.getStringExtra(“国家”);
试一试{
最终字符串BASE_URL=”http://1.1.1.1:8080/WebServices/country/";
uribuilduri=Uri.parse(BASE_URL).buildons()
.appendPath(chosenCountry).build();
URL=新URL(buildUri.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
//将输入流读入字符串
InputStream InputStream=urlConnection.getInputStream();
StringBuffer=新的StringBuffer();
如果(inputStream==null){
//无事可做。
返回;
}
reader=新的BufferedReader(新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
//因为它是JSON,所以不需要添加换行符(这不会影响解析)
//但是如果你打印出完成的代码,调试会变得容易得多
//用于调试的缓冲区。
buffer.append(第+行“\n”);
}
if(buffer.length()==0){
//流为空。在分析中没有意义。
返回;
}
NumbersJSON=buffer.toString();
JSONArray Numbers_Array=新的JSONArray(NumbersJSON);
对于(int i=0;Numbers\u Array.length()!=0&&i
选择号码

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.a2ndnumber.a2ndnumber.service.GetNumberService;

import java.util.ArrayList;

public class Choose_number extends AppCompatActivity {

    static ArrayList<String> mNumbersList;

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(GetNumberService.ABC)){
                mNumbersList = intent.getStringArrayListExtra("NumberList");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_number);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        IntentFilter filter = new IntentFilter();
        filter.addAction(GetNumberService.ABC);
        LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this);
        bm.registerReceiver(mBroadcastReceiver, filter);
    }

}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.support.v4.content.LocalBroadcastManager;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入com.a2ndnumber.a2ndnumber.service.GetNumberService;
导入java.util.ArrayList;
公共类Choose_编号扩展AppCompatActivity{
静态数组列表mNumbersList;
专用最终广播接收器mBroadcastReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
if(intent.getAction().equals(GetNumberService.ABC)){
mnumberlist=intent.getStringArrayListExtra(“NumberList”);
}
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u选择\u编号);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
IntentFilter=newintentfilter();
filter.addAction(GetNumberService.ABC);
LocalBroadcastManager bm=LocalBroadcastManager.getInstance(this);
bm.寄存器接收器(MBroadcasterreceiver,过滤器);
}
}
选择数字片段

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A placeholder fragment containing a simple view.
 */
public class Choose_numberFragment extends Fragment {


    private ArrayAdapter<String> NumberListAdapter;

    public Choose_numberFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        NumberListAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_numbers, R.id.list_item_textview, Choose_number.NumbersList);
        View rootView = inflater.inflate(R.layout.fragment_number, container, false);

        ListView listView = (ListView) rootView.findViewById(R.id.listview_List);
        listView.setAdapter(mNumberListAdapter);

        return rootView;
    }

}
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
/**
*包含简单视图的占位符片段。
*/
公共类Choose\u numberFragment扩展片段{
专用阵列适配器编号STADAPTER;
公共C