Android app widget listview onclick listener
Asked Answered
D

1

6

I am new to android development. I am trying to make a widget for my app which will contain a listview, with each listview having 2 buttons apart from heading and content.

I am trying to hide the corresponding element of the listitem in the app widget(on homescreen) when the button of that element is pressed.

First I tried to check if the button click is getting any response and if I could detect if I can get the position of the item. 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 the 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);

The mainActivity.java is the default activity of the program, where I did this to get the item position:

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

and I am thus able to open the main activity on a button click and getting a TOAST text in the bottom, giving exact result.

But I wish to hide/disable the corresponding element of the app widget when clicked. What should I do for that?

Drinker answered 24/7, 2014 at 9:53 Comment(0)
U
0

you must remove list item from the adapter when the button is pressed. And call

adapter.notifyDataSetChanged();

then the item will disappear. Hopes this can help.

Unfavorable answered 12/11, 2015 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.