1

Estoy intentando aprender a hacer Widgets y quiero hacer uno con un listView.

En la Activity normal de la app yo hago lo típico de:

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

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

lv.setAdapter(adapter);

Pero cuando lo intento en el código del widget no me deja hacerlo

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>

¿Cómo lo tengo qué hacer?

7
  • como que no te deja hacerlo? te da algún error? el 9 jun. 2017 a las 9:57
  • Si, claro, directamente al poner findViewById() me sale en rojo, y si le doy a sugerencias (alt+intro) me dice que cree el método...
    – user36042
    el 9 jun. 2017 a las 10:09
  • Está implementado en tu layout? el 9 jun. 2017 a las 10:16
  • Si, añadí el codigo del layout
    – user36042
    el 9 jun. 2017 a las 10:25
  • puedes añadir el manifest también? el 9 jun. 2017 a las 10:41

1 respuesta 1

0

En tu proveedor:

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

Si cada objeto en tu ListView necesita un extra diferente para ayudar al onReceive, crea el Intent necesario, puedes usar el FillInIntent por cada objeto o item en el ListView, en 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;
1
  • Sorry, pero no entiendo muy bien donde tengo que meter cada cosa... es decir, el onRecive si, aunque no se qué tengo que poner en ACTION_NAME ni que Activity tengo que abrir... Del resto, ni idea de cómo va...
    – user36042
    el 9 jun. 2017 a las 11:31

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.