Android app widget onclick-listener

I am new to Android development. I am trying to make a widget for my application that will contain a list, with each list having two buttons, except for the title and content.

I am trying to hide the corresponding listitem element in application widgets (on the main screen) when the button for this element is clicked.

At first I tried to check if there was any answer on the button, and if I could determine if I can get the position of the element. So I did this:

listprovider.java (implements remoteviewfactory) :

@Override
    public RemoteViews getViewAt(int position) {
        final RemoteViews remoteView = new RemoteViews(
                context.getPackageName(), R.layout.list_row);
        ListItem listItem = listItemList.get(position);
        remoteView.setTextViewText(R.id.heading, listItem.heading);
        remoteView.setTextViewText(R.id.content, listItem.content);

        Bundle extras = new Bundle();
        extras.putInt(WidgetProvider.EXTRA_ITEM, position);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        // Make it possible to distinguish the individual on-click
        // action of a given item
        remoteView.setOnClickFillInIntent(R.id.buttonwidget, fillInIntent);

        return remoteView;
    }

and then in WidgetProvider.java (extends AppWidgetProvider) :

for (int i = 0; i < N; ++i) {
            RemoteViews remoteViews = updateWidgetListView(context,
                    appWidgetIds[i]);
            Intent clickIntent = new Intent(context, MainActivity.class);
            clickIntent.setAction(WidgetProvider.TOAST_ACTION);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            clickIntent.setData(Uri.parse(clickIntent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent clickPI = PendingIntent.getActivity(context, 0,
                    clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            remoteViews.setPendingIntentTemplate(com.example.markup.R.id.listViewWidget, clickPI);
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);

mainActivity.java - , , :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Printing detail of clicked item from widget
        Intent intent = getIntent();
        if (intent.getAction().equals(WidgetProvider.TOAST_ACTION)) {
            int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
            int viewIndex = intent.getIntExtra(WidgetProvider.EXTRA_ITEM, 0);
            Toast.makeText(getApplicationContext(), "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "Touched view none ", Toast.LENGTH_SHORT).show();
        }
    }

TOAST , .

/ . ?

+4
1

.

adapter.notifyDataSetChanged();

. , .

0

All Articles