Member Avatar for Kubilay Doğukan

I'm getting my ArrayList from a service and populate my ListView with it when user creates the widget. I have 3 button on my widget and I want to modify/filter the data in the ListView by button click. I saw examples of pending indent but I couldn't reach my ArrayList (I couldn't figure out how to reach). I'm providing codes that are relevant. AppWidgetProvider:

public class MudoLifeWidget extends AppWidgetProvider {

    private static final String Button1_OnClick = "Button1_OnClickTag";
    private static final String Button2_OnClick = "Button2_OnClickTag";
    private static final String Button3_OnClick = "Button3_OnClickTag";



    protected PendingIntent getPendingSelfIntent(Context context, String action){
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context,0,intent,0);
    }


    @Override
    public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them


        try {
            for (int widgetId : appWidgetIds) {
                RemoteViews mView = initViews(context, appWidgetManager, widgetId);
                mView.setOnClickPendingIntent(R.id.button1,getPendingSelfIntent(context,Button1_OnClick));
                mView.setOnClickPendingIntent(R.id.button2,getPendingSelfIntent(context,Button2_OnClick));
                mView.setOnClickPendingIntent(R.id.button3,getPendingSelfIntent(context,Button3_OnClick));
                appWidgetManager.updateAppWidget(widgetId, mView);
            }

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


    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private RemoteViews initViews(Context context,
                                  AppWidgetManager widgetManager, int widgetId) {

        RemoteViews mView = new RemoteViews(context.getPackageName(),
                R.layout.mudo_life_widget);

        Intent intent = new Intent(context, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        mView.setRemoteAdapter(widgetId, R.id.list, intent);

        return mView;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if(Button1_OnClick.equals(intent.getAction())){
            Toast.makeText(context, "Category1", Toast.LENGTH_SHORT).show();
            //Filter 1

        }

        else if(Button2_OnClick.equals(intent.getAction())){
            Toast.makeText(context, "Category2", Toast.LENGTH_SHORT).show();
            //Filter 2
        }

        else if(Button3_OnClick.equals(intent.getAction())){
            Toast.makeText(context, "Category3", Toast.LENGTH_SHORT).show();
            //Filter 3
        }

    }
}

WidgetDataProvider:

@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 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 void onCreate() {
        initDataFromService();


    }

    @Override
    public void onDataSetChanged() {
        initDataFromService();

    }


    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();

        try {
            Void result =asyncanlikcheck.execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    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();
        }
    }


}

WidgetService:

@SuppressLint("NewApi")
public class WidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {

        WidgetDataProvider dataProvider = new WidgetDataProvider(
                getApplicationContext(), intent);
        return dataProvider;
    }

}

Any help or guidance is much appreciated. Thanks.

Member Avatar for Kubilay Doğukan

I've tried adding WidgetDataProvider.modifData(); to Filter1 and added

public static void modifData(){
        try {
            Collections.sort(mCollections, Anlik.COMPARE_BY_BIRIM);
            WidgetDataProvider WDP = new WidgetDataProvider(mContext,mIntent);
            WDP.onDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

to WidgetDataProvider but nothing happened.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.