2017-01-24 3 views
1

Webから読み込まれる数行のデータを表示する私のApp用のウィジェットを作成したいと思います。ListViewカスタムアイテムを使用したAndroid Widget

私は、リストビュー、AppWidgetProvider、RemoteViewsService、およびRemoteViewsFactoryを含むウィジェットレイアウトを持っています。サービスはマニフェストに登録されます。

問題は、カスタマイズされたListItemレイアウトを使用できないことです。 public RemoteViews getViewAt(int position)で動作する唯一のものは、多くの基本チュートリアルで行われたようにandroid.R.layout.simple_list_item_1を使用しています。しかし、自分のレイアウトを使用しようとすると、各エントリ(デフォルトの読み込みビュー)の「読み込み中...」というエントリが表示されます。ここ
は、私が使用したいレイアウトです:

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

     <TextView 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="@dimen/cardview_spacing" 
      android:layout_marginEnd="@dimen/cardview_spacing" 
      android:layout_gravity="center_horizontal" 
      android:textSize="@dimen/textTitle" 
      android:id="@+id/plan_entry_lesson" /> 

     <TextView 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="@dimen/cardview_spacing" 
      android:layout_marginEnd="@dimen/cardview_spacing" 
      android:layout_gravity="center_horizontal" 
      android:textSize="@dimen/textTitle" 
      android:id="@+id/plan_entry_class" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:textSize="@dimen/textSmallTitle" 
      android:id="@+id/plan_header" 
      android:text="@string/plan_header"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="@dimen/cardview_spacing" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:id="@+id/plan" 
      android:layout_marginLeft="@dimen/cardview_spacing" /> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:textSize="@dimen/textSmallTitle" 
      android:id="@+id/subst_header" 
      android:text="@string/subst_header"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="@dimen/cardview_spacing" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:id="@+id/subst" 
      android:layout_marginLeft="@dimen/cardview_spacing" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:textSize="@dimen/textSmallTitle" 
      android:id="@+id/comment_header" 
      android:text="@string/comment_header"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="@dimen/cardview_spacing" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_gravity="start" 
      android:id="@+id/comment" 
      android:layout_marginLeft="@dimen/cardview_spacing" /> 

    </LinearLayout> 
</LinearLayout> 

データは、私は、データがあると、データはすべて、またはこのようなものでは存在しませんでしたが、私のデバッグ出力が表示されていることを示唆非同期でロードされているので、私が作成するビューはnullではありません。しかし、私はアンドロイドのサンプルリスト項目しか使用できません。

これに関するご提案はありますか?

私のgetViewTypeCount()は、現時点では1を返します!(未0共通の問題であるように思われた)

完全性のために:RemoteViewsFactory:

public class WidgetDataProvider implements RemoteViewsService.RemoteViewsFactory { 
    private static final String TAG = "WidgetDataProvider"; 
    private Vplan plan; 

    private List<VplanEntry> mCollection = new ArrayList<>(); 
    private Context mContext = null; 

    public WidgetDataProvider(Context context, Intent intent) { 
     mContext = context; 
     plan = new Vplan(mContext, "today"); 
    } 

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

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

    @Override 
    public void onDestroy() { 

    } 

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

    @Override 
    public RemoteViews getViewAt(int position) { 
     Log.d("WIDGET", "getViewAt " + position); 
     VplanEntry entry = mCollection.get(position); 
     Log.d("WIDGET", "entry plan: " + entry.getPlan()); 

     RemoteViews view = new RemoteViews(mContext.getPackageName(), 
       android.R.layout.simple_list_item_1); 
     String text = entry.getCls() + " " + entry.getLesson() + ":\n" 
       + entry.getPlan() +"\n" 
       + entry.getSubstitution() + "\n" 
       + entry.getComment(); 
     view.setTextViewText(android.R.id.text1, text); 
     //view.setTextViewText(R.id.plan_entry_lesson, entry.getLesson()); 

     return view; 
    } 

    @Override 
    public RemoteViews getLoadingView() { 
     //return new RemoteViews(mContext.getPackageName(), 
     //  R.layout.vplan_appwidget_loading); 
     return null; 
    } 

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

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

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

    private void initData() { 
     mCollection.clear(); 
     if (plan.isLoaded() && plan.upToDate()) { 
      mCollection.clear(); 
      Log.d("WIDGET", "Data present"); 
      for (VplanEntry entry : plan.getEntries()) { 
       mCollection.add(entry); 
      } 
      Log.d("WIDGET", "Entries: " + mCollection.size()); 
      AppWidgetManager awm = AppWidgetManager.getInstance(mContext); 
      awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(new ComponentName(mContext, WidgetService.class)), R.id.widgetListView); 
     } else { 
      Log.d("WIDGET", "Loading started"); 
      plan.load(new SuccessCallback() { 
       @Override 
       public void callback(boolean success) { 
        Log.d("WIDGET", "Loading finished: " + success); 
        if (success) { 
         initData(); 
        } 
       } 
      }); 
     } 

    } 
} 

答えて

0

は、問題は、私はレイアウトが最低限必要なウィジェットの幅よりも幅を持っていたということでした...

幅を変更すると問題が解決しました。