2017-07-30 91 views
0

我正在創建一個Android應用程序,該應用程序應該顯示小部件中的項目列表(字符串),但出於某種原因,我無法使其工作。Android Widget不顯示數據

代碼

的widget成分

package com.example.vamshi.baking.Widget; 

import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.widget.RemoteViews; 

import com.example.vamshi.baking.R; 
import com.example.vamshi.baking.Widget.Service.WidgetService; 

/** 
* Implementation of App Widget functionality. 
*/ 
public class Ingredients extends AppWidgetProvider { 

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
           int appWidgetId) { 

     CharSequence widgetText = context.getString(R.string.appwidget_text); 
     // Construct the RemoteViews object 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ingredients); 


     // Instruct the widget manager to update the widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     final int N = appWidgetIds.length; 
     for (int i = 0; i<N; ++i) { 
      RemoteViews remoteViews = updateWidgetListView(context, 
        appWidgetIds[i]); 
      appWidgetManager.updateAppWidget(appWidgetIds[i], 
        remoteViews); 
     } 

    } 

    private RemoteViews updateWidgetListView(Context context, int appWidgetId) { 
     RemoteViews remoteViews = new RemoteViews(
       context.getPackageName(),R.layout.ingredients); 

     //RemoteViews Service needed to provide adapter for ListView 
     Intent svcIntent = new Intent(context, WidgetService.class); 
     //passing app widget id to that RemoteViews Service 
     svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
     //setting a unique Uri to the intent 
     //don't know its purpose to me right now 
     svcIntent.setData(Uri.parse(
       svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
     //setting adapter to listview of the widget 
     remoteViews.setRemoteAdapter(R.id.list_view_widget, 
       svcIntent); 

     return remoteViews; 
    } 

    @Override 
    public void onEnabled(Context context) { 
     // Enter relevant functionality for when the first widget is created 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // Enter relevant functionality for when the last widget is disabled 
    } 
} 

小部件服務

package com.example.vamshi.baking.Widget.Service; 

import android.appwidget.AppWidgetManager; 
import android.content.Intent; 
import android.widget.RemoteViewsService; 

import com.example.vamshi.baking.Widget.WidgetViewAdapter; 

/** 
* Created by Vamshi on 7/27/2017. 
*/ 

public class WidgetService extends RemoteViewsService { 

    @Override 
    public RemoteViewsFactory onGetViewFactory(Intent intent) { 
     int appWidgetID = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
       AppWidgetManager.INVALID_APPWIDGET_ID); 
     return (new WidgetViewAdapter(this.getApplicationContext(), intent)); 
    } 
} 

小工具視圖適配器

package com.example.vamshi.baking.Widget; 

import android.appwidget.AppWidgetManager; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.RemoteViews; 
import android.widget.RemoteViewsService; 
import android.widget.Toast; 

import com.example.vamshi.baking.Data.*; 
import com.example.vamshi.baking.Data.Ingredients; 
import com.example.vamshi.baking.R; 
import com.example.vamshi.baking.Retrofit.IRecipe; 
import com.example.vamshi.baking.Retrofit.RetrofitBuilder; 

import java.util.ArrayList; 

import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 

/** 
* Created by Vamshi on 7/27/2017. 
*/ 

public class WidgetViewAdapter implements RemoteViewsService.RemoteViewsFactory { 

    Context context; 
    ArrayList<Recipe> r; 
    ArrayList<com.example.vamshi.baking.Data.Ingredients> i; 
    private int appWidgetID; 
    ArrayList<String> iList; 

    public WidgetViewAdapter(Context c, Intent in){ 
     populate(); 
     this.context = c; 
     appWidgetID = in.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
       AppWidgetManager.INVALID_APPWIDGET_ID); 

    } 

    private void populate() { 

     IRecipe irecipie = RetrofitBuilder.Retrieve(); 
     final Call<ArrayList<Recipe>> recipie = irecipie.getRecipe(); 
     recipie.enqueue(new Callback<ArrayList<Recipe>>() { 
      @Override 
      public void onResponse(Call<ArrayList<Recipe>> call, Response<ArrayList<Recipe>> response) { 
       r = response.body(); 
       i = r.get(0).getIngredients(); 
       for(int j = 0; j<=i.size(); j++){ 
        iList.add(i.get(0).getIngredient()); 
       } 
      } 

      @Override 
      public void onFailure(Call<ArrayList<Recipe>> call, Throwable t) { 
       Toast.makeText(context, "No Internet Connection", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    @Override 
    public void onCreate() { 

     populate(); 
    } 

    @Override 
    public void onDataSetChanged() { 

     IRecipe irecipie = RetrofitBuilder.Retrieve(); 
     final Call<ArrayList<Recipe>> recipie = irecipie.getRecipe(); 
     recipie.enqueue(new Callback<ArrayList<Recipe>>() { 
      @Override 
      public void onResponse(Call<ArrayList<Recipe>> call, Response<ArrayList<Recipe>> response) { 
       r = response.body(); 
       i = r.get(0).getIngredients(); 
       for(int j = 0; j<=i.size(); j++){ 
        iList.add(i.get(0).getIngredient()); 
       } 
      } 

      @Override 
      public void onFailure(Call<ArrayList<Recipe>> call, Throwable t) { 
       Toast.makeText(context, "No Internet Connection", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 

    @Override 
    public void onDestroy() { 

    } 

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

    @Override 
    public RemoteViews getViewAt(int position) { 
     final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.row); 
     Ingredients in = i.get(position); 
     remoteView.setTextViewText(R.id.text_for_widget, in.getIngredient()); 
     return remoteView; 
    } 

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

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

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

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

清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.vamshi.baking"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <service 
     android:name=".WidgetService" 
     android:permission="android.permission.BIND_REMOTEVIEWS" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".UI.MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".UI.SecondScreenDetails" /> 
     <activity android:name=".UI.StepsActivity" /> 
     <activity android:name=".UI.SingleStepItem" /> 

     <receiver android:name=".Widget.BakingWidget"> 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/baking_widget_info" /> 
     </receiver> 
     <service 
      android:name=".Widget.Service.WidgetService" 
      android:permission="android.permission.BIND_REMOTEVIEWS" /> 

     <activity android:name=".UI.MasterDetailFlow" /> 

     <receiver android:name=".Widget.Ingredients"> 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/ingredients_info" /> 
     </receiver> 
    </application> 

</manifest> 

佈局文件

Ingredients.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin"> 

    <ListView 
     android:id="@+id/list_view_widget" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </ListView> 

</LinearLayout> 

row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/text_for_widget" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

在此先感謝

+0

請仔細閱讀[在什麼情況下可能我在orde中添加了「緊急」或其他類似的短語來解決我的問題以獲得更快的答案?](// meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者的理想方式,並且可能適得其反。請不要將這添加到您的問題。 – halfer

回答

0

我的目標是在小部件 顯示字符串列表和自部件不允許顯示 回收的觀點我不得不去的列表視圖 我有相當多的麻煩找出究竟是什麼問題 但我得到了提到了一個驚人的一篇文章,充分表明了該怎麼辦呢 我在這裏張貼的鏈接,使櫃面任何你需要幫助,可以參考這個

https://dharmangsoni.blogspot.in/2014/03/collection-widget-with-event-handling.html

0

在您的小工具中查看適配器使getViewTypeCount回報1.

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

此方法提供了提供該工廠的視圖類型的數量。 也更好地在IntentService中進行網絡調用,並在RemoteViewsService的意圖中傳遞響應主體 ,並且此服務在ViewsFactory中傳遞該意圖,此後ViewsFactory可以填充成分列表。