ListView更新一个ArrayList但不适用于其他[Widget]

时间:2016-04-11 07:49:13

标签: java android listview android-appwidget

我正在尝试在appwidget中更新我的ListView我可以看到列表已创建如果我使用initData()而不是initDataFromService()。我在两个函数中使用相同类型的数据填充mCollection。我无法弄清楚有什么区别?为什么不更新? 感谢。

我可以提供所需的任何信息。

这是WidgetDataProvider:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

@SuppressLint("NewApi")
public class WidgetDataProvider implements RemoteViewsFactory {

    SharedPreferences sharedPreferences;
    String phonenumber;
    String personelno;

    ArrayList<Anlik> mCollections = new ArrayList<Anlik>();

    Context mContext = null;

    public WidgetDataProvider(Context context, Intent intent) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mCollections.size();
    }

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

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews mView = new RemoteViews(mContext.getPackageName(),
                android.R.layout.simple_list_item_1);
        mView.setTextViewText(android.R.id.text1, mCollections.get(position).toString());
        mView.setTextColor(android.R.id.text1, Color.BLACK);
        return mView;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onCreate() {
        initData();
    }

    @Override
    public void onDataSetChanged() {
        initData();
    }

    private void initData() {

        for (int i = 1; i <= 10; i++) {
            mCollections.add(new Anlik("a","b","c","d","e","f","g","h","j"));
        }
    }

    private void initDataFromService() {

        sharedPreferences = mContext.getSharedPreferences("PREFERENCES", mContext.getApplicationContext().MODE_PRIVATE);
        phonenumber = sharedPreferences.getString("mPhonenumber", "");
        personelno = sharedPreferences.getString("mPersonalno", "");
        class AsyncAnlikCheck extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                anlikService(phonenumber, personelno);
                return null;
            }
        }
        AsyncAnlikCheck asyncanlikcheck = new AsyncAnlikCheck();
        asyncanlikcheck.execute();*/

    }

    public void anlikService(String phonenumber,String personalno){
        SoapObject request = new SoapObject(LoginActivity.NAMESPACE,"MudoLife_getAnlikSatisAuth");

        request.addProperty("phoneNumber",phonenumber);
        request.addProperty("personelNumber",personalno);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE transportSE = new HttpTransportSE(LoginActivity.URL);

        transportSE.debug=true;

        try{

            transportSE.call("http://tempuri.org/MudoLife_getAnlikSatisAuth",envelope);
            final SoapObject response = (SoapObject) envelope.getResponse();
            if(response.getPropertyCount()!=0){

                for(int i=0;i<response.getPropertyCount();i++){
                    SoapObject anliklistt = (SoapObject) response.getProperty(i);
                    mCollections.add(new Anlik(anliklistt.getProperty("urnGrp").toString(),anliklistt.getProperty("ttr").toString(),anliklistt.getProperty("ttrGY").toString(),anliklistt.getProperty("degisim").toString(),anliklistt.getProperty("resim").toString(),anliklistt.getProperty("marj").toString(),anliklistt.getProperty("marjGY").toString(),anliklistt.getProperty("marjDegisim").toString(),anliklistt.getProperty("marjResim").toString()));
                }
                //Collections.sort(mCollections, Anlik.COMPARE_BY_BIRIM);

            }

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

    @Override
    public void onDestroy() {

    }

}

0 个答案:

没有答案