ListView on Widget

1

I'm trying to learn how to make Widgets and I want to make one with a listView.

In the normal Activity of the app I do the typical of:

ListView lv = (ListView) findViewById(R.id.lv);

AdapterLV adapter = new AdapterLV(getContext(),R.layout.fila, arrayList);

lv.setAdapter(adapter);

But when I try it in the widget code it does not let me do it

public class MiWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context,
                         AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {

        //Aquí intento el findViewById y todo lo demás...

    }
}

<FrameLayout
    android:id="@+id/frmWidget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:padding="5dp">


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

Manifest:

<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=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".MiWidget" android:label="Mi Primer Widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/miwidget_wprovider" />
    </receiver>
</application>

How do I have to do it?

    
asked by Jmyebenes 09.06.2017 в 11:21
source

1 answer

0

In your provider:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equals(ACTION_NAME)) {
        // Abre tu actividad aquí.
    }
}

If each object in your ListView needs a different extra to help onReceive , create the necessary Intent, you can use FillInIntent for each object or item in ListView , in getViewAt :

// Dale valor a onClickFillInIntent
final Intent fillInIntent = new Intent();
final Bundle extras = new Bundle();
extras.putInt(YourWidgetProvider.ACTION_EXTRA_ITEM, event.getId()); // retrieve this in onReceive with intent.getIntExtra(ACTION_EXTRA_ITEM, -1)
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.listview_main_layout, fillInIntent);
return rv;
    
answered by 09.06.2017 в 13:25