How do I show the latest book entered in the Android Studio widget?

0

Hello, I'm a beginner in Android Studio and would like help. I'm making an app from a virtual library and I can not show the books registered in the widget. I followed a tutorial but I lost.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.edu.unp.bibliotecavirtual">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".view.ListarLivrosActivity" />
        <activity android:name=".view.AtualizaDeletaActivity" />
        <activity android:name=".view.AddBookActivity" />
        <activity android:name=".view.GiveBackBookActivity" />
        <activity android:name=".view.GiveOutBookActivity" />
        <activity android:name=".view.SearchBookActivity" />

        <receiver android:name=".view.BroadcastReceiver1" />
        <receiver android:name=".BibliotecaVirtualWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/biblioteca_virtual_widget_info" />
        </receiver>

        <activity android:name=".BibliotecaVirtualWidgetConfigureActivity">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/biblioteca_virtual_widget_info" />
        </activity>
    </application>

</manifest>

Virtual LibraryWidget.java

package br.edu.unp.bibliotecavirtual;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;

/**
 * Implementation of App Widget functionality.
 * App Widget Configuration implemented in {@link BibliotecaVirtualWidgetConfigureActivity BibliotecaVirtualWidgetConfigureActivity}
 */
public class BibliotecaVirtualWidget extends AppWidgetProvider {

    private static final String mSharedPrefFile = "com.example.android.appwidgetsample";
    private static final String COUNT_KEY = "count";

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId) {

        SharedPreferences prefs = context.getSharedPreferences(mSharedPrefFile, 0);
        int count = prefs.getInt(COUNT_KEY + appWidgetId, 0);
        count++;

        String dateString = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());

        CharSequence widgetText = BibliotecaVirtualWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.biblioteca_virtual_widget);
        views.setTextViewText(R.id.appwidget_text, widgetText);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);

        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        for (int appWidgetId : appWidgetIds) {
            BibliotecaVirtualWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
        }
    }

}

Virtual LibraryWidgetConfigureActivity.java

package br.edu.unp.bibliotecavirtual;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

/**
 * The configuration screen for the {@link BibliotecaVirtualWidget BibliotecaVirtualWidget} AppWidget.
 */
public class BibliotecaVirtualWidgetConfigureActivity extends Activity {

    private static final String PREFS_NAME = "br.edu.unp.bibliotecavirtual.BibliotecaVirtualWidget";
    private static final String PREF_PREFIX_KEY = "appwidget_";
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    EditText mAppWidgetText;
    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            final Context context = BibliotecaVirtualWidgetConfigureActivity.this;

            // When the button is clicked, store the string locally
            String widgetText = mAppWidgetText.getText().toString();
            saveTitlePref(context, mAppWidgetId, widgetText);

            // It is the responsibility of the configuration activity to update the app widget
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            BibliotecaVirtualWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    };

    public BibliotecaVirtualWidgetConfigureActivity() {
        super();
    }

    // Write the prefix to the SharedPreferences object for this widget
    static void saveTitlePref(Context context, int appWidgetId, String text) {
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
        prefs.apply();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    // If there is no preference saved, get the default from a resource
    static String loadTitlePref(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
        if (titleValue != null) {
            return titleValue;
        } else {
            return context.getString(R.string.appwidget_text);
        }
    }

    static void deleteTitlePref(Context context, int appWidgetId) {
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.remove(PREF_PREFIX_KEY + appWidgetId);
        prefs.apply();
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if the user presses the back button.
        setResult(RESULT_CANCELED);

        setContentView(R.layout.biblioteca_virtual_widget_configure);
        mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
        findViewById(R.id.add_button).setOnClickListener(mOnClickListener);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If this activity was started with an intent without an app widget ID, finish with an error.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
            return;
        }

        mAppWidgetText.setText(loadTitlePref(BibliotecaVirtualWidgetConfigureActivity.this, mAppWidgetId));
    }
}

virtual_widget.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#09C"
    android:padding="@dimen/widget_margin">

    <RelativeLayout
        android:id="@+id/btnAdicionar"
        style="@style/AppWidgetSection"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="12dp"
        android:orientation="vertical"
        tools:context=".view.ListarLivrosActivity">

        <ImageView
            android:id="@+id/btnPesquisar"
            android:layout_width="80dp"
            android:layout_height="115dp"
            android:layout_alignParentRight="true"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/appwidget_update_label"
            style="@style/AppWidgetLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="5dp"
            android:text="@string/ultimo_cadastro"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/appwidget_update"
            style="@style/AppWidgetText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/btnPesquisar"
            android:layout_alignStart="@+id/appwidget_update_label"
            android:text="@string/date_count_format"
            android:textColor="#000000"
            android:textSize="16sp" />

    </RelativeLayout>

    <Button
        android:id="@+id/btnAtualizar"
        android:layout_width="131dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnAdicionar"
        android:layout_centerHorizontal="true"
        android:text="Atualizar" />

</RelativeLayout>

virtual_widget_configure.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="@string/configure" />

    <EditText
        android:id="@+id/appwidget_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <Button
        android:id="@+id/add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="@string/add_widget" />

</LinearLayout>

virtual_widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="br.edu.unp.bibliotecavirtual.BibliotecaVirtualWidgetConfigureActivity"
    android:initialKeyguardLayout="@layout/biblioteca_virtual_widget"
    android:initialLayout="@layout/biblioteca_virtual_widget"
    android:minHeight="80dp"
    android:minWidth="150dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="1800000"
    android:widgetCategory="home_screen|keyguard" />

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    tools:context=".view.MainActivity">

    <include android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


    <ImageView
        android:id="@+id/logoId"
        android:layout_width="260dp"
        android:layout_height="140dp"
        android:layout_below="@+id/toolbar"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo" />

    <ImageView
        android:id="@+id/btnEmprestar"
        android:layout_width="80dp"
        android:layout_height="115dp"
        android:layout_alignStart="@+id/btnPesquisar"
        android:layout_centerVertical="true"
        android:src="@drawable/out_book"
        android:onClick="chmaraBR"/>

    <ImageView
        android:id="@+id/btnPesquisar"
        android:layout_width="80dp"
        android:layout_height="115dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/logoId"
        android:layout_marginBottom="51dp"
        android:src="@drawable/search_book"
        android:onClick="chmaraBR"/>

    <ImageView
        android:id="@+id/btnDevolver"
        android:layout_width="80dp"
        android:layout_height="115dp"
        android:layout_alignStart="@+id/btnAdicionar"
        android:layout_alignTop="@+id/btnEmprestar"
        android:src="@drawable/in_book"
        android:onClick="chmaraBR"/>


    <ImageView
        android:id="@+id/btnAdicionar"
        android:layout_width="80dp"
        android:layout_height="115dp"
        android:layout_alignEnd="@+id/logoId"
        android:layout_alignTop="@+id/btnPesquisar"
        android:src="@drawable/add_book"
        android:onClick="chmaraBR"/>


</RelativeLayout>
    
asked by anonymous 21.05.2018 / 00:06

0 answers