App Widget issue while showing list of images

跟風遠走 提交于 2019-12-20 05:46:21

问题


I go through the following android documentation: "App Widgets with Collections" to create a widget which show list of text. Everything works fine and its work just as i wanted it to work.

Now instead of showing list of text i want to show list of image that are stored in a sd-card. So how that can be done in my code that i have implemented. If anyone have some idea on this,Please help me to sort this out.

The code i written is

WidgetProvider.java

public class WidgetProvider extends AppWidgetProvider 
{
    public static String EXTRA_WORD=
            "com.commonsware.android.appwidget.lorem.WORD";

    @Override
    public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) 
    {
        for (int i=0; i<appWidgetIds.length; i++) 
        {
            Intent svcIntent=new Intent(ctxt, WidgetService.class);

            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
                    R.layout.widget);

            widget.setRemoteAdapter(appWidgetIds[i], R.id.words,
                    svcIntent);

            Intent clickIntent=new Intent(ctxt, LoremActivity.class);
            PendingIntent clickPI=PendingIntent
                    .getActivity(ctxt, 0,
                            clickIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

            widget.setPendingIntentTemplate(R.id.words, clickPI);

            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
        }

        super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
    }
}

WidgetService.java

public class WidgetService extends RemoteViewsService 
{
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent)
    {
        return(new WidgetDisplay(this.getApplicationContext(),
                intent));
    }
}

WidgetDisplay.java

public class WidgetDisplay implements RemoteViewsService.RemoteViewsFactory 
{
    private static final String[] items={"Red", "Green", "Blue", "Pink", "Orange"};
    private Context ctxt=null;
    private int appWidgetId;

    public WidgetDisplay(Context ctxt, Intent intent) 
    {
        this.ctxt=ctxt;
        appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public void onCreate() 
    {
        // no-op
    }

    @Override
    public void onDestroy() 
    {
        // no-op
    }

    @Override
    public int getCount() 
    {
        return(items.length);
    }

    @Override
    public RemoteViews getViewAt(int position) 
    {
        RemoteViews row=new RemoteViews(ctxt.getPackageName(),
                R.layout.row);

        row.setTextViewText(android.R.id.text1, items[position]);

        Intent i=new Intent();
        Bundle extras=new Bundle();

        extras.putString(WidgetProvider.EXTRA_WORD, items[position]);
        i.putExtras(extras);
        row.setOnClickFillInIntent(android.R.id.text1, i);

        return(row);
    }

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

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

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

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

    @Override
    public void onDataSetChanged() 
    {
        // no-op
    }
}

XML File

Widget.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/words"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="3dp"
  android:layout_marginLeft="3dp"
  android:background="@drawable/widget_frame"
/>

Row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"   
/>

Widget_provider.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="146dip"
  android:minHeight="146dip"
  android:updatePeriodMillis="0"
  android:initialLayout="@layout/widget"
  android:autoAdvanceViewId="@+id/words"
  android:previewImage="@drawable/preview"
  android:resizeMode="vertical"
/>

ScreenShots of the widget that i got


回答1:


I am used this way and work well.

Change textview to imageview in row.xml

public WidgetDisplay(Context ctxt, Intent intent) 
    {
        this.ctxt=ctxt;
        appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
                setImageinView(this.ctxt);
    }

private void setImageinView(Context context) {
//read sd card and store bitmap in  arrayListBitmap;
}

@Override
public RemoteViews getViewAt(int position) {
remoteView = new RemoteViews(ctxt.getPackageName(), R.layout.row);
    remoteView.setImageViewBitmap(R.id.image_photo1,arrayListBitmap.get(i));
}

simple you have to read sd card store data in to arraylist.

set size to getCount().

now set image in your imageview.

If any query then welcome.



来源:https://stackoverflow.com/questions/15523736/app-widget-issue-while-showing-list-of-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!