应用程序窗口小部件的问题,同时显示的图像列表部件、应用程序、图像、窗口

2023-09-08 00:03:12 作者:阳光一点的女生

我通过以下的android文档:应用程序的窗口小部件与收藏,以创建一个控件,它显示的文本列表。一切工作正常,它的工作,就像我想它的工作。

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.

现在,而不是显示文本​​列表我要显示存储在SD卡图像列表。 那么,如何能在我,我已经实现了code来完成。如果任何人有这方面的一些想法,请帮我整理了这一点。

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.

的code我写的是

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文件

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"
/>

,我得到了

推荐答案

我用这样的方式,工作好。

I am used this way and work well.

更改的TextView来的ImageView中的 row.xml

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

简单,你必须读SD卡存储数据到数组列表。

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

集大小为 getCount将()

现在设置图像的ImageView的。

now set image in your imageview.

如果任何查询,欢迎届时。

If any query then welcome.