2

I want to show some list of TextView and EditView in my Widget.

I used the following code to generate list of TextView and EditView

public LinearLayout getMainBodyLayout(List<Item> data) {
        LinearLayout mainLL = new LinearLayout(context);
        mainLL.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < data.size(); i++) {
            Item ritem = data.get(i);
            LinearLayout item = new LinearLayout(context);
            TextView name = new TextView(context);
            EditText nos = new EditText(context);
            name.setText(ritem.getName());
            nos.setText(ritem.getNo());
            item.addView(name);
            item.addView(nos);
            mainLL.addView(item);

        }

        return mainLL;
    }

public void updateWidget(LinearLayout ll) {
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, MainWidget.class);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_my);
        remoteViews.setTextViewText(R.id.title, title);

        remoteViews.setTextViewText(R.id.widget_error, errorMsg);

// I ve to add ll to this remoteViews


        manager.updateAppWidget(thisWidget, remoteViews);
    }

I have to add the returned LinearLayout to my widget Linearlayout. Please provide me the best way to do this.

Sridhar
  • 2,228
  • 10
  • 48
  • 79
  • 1
    Do you want to make a list???? If so why dont you use a listview for that any issue with that ?? And listview will be faster than what you are doing right now. – BBdev Jul 02 '13 at 09:53
  • @BBdev, I didnt know how to set adapter for that... – Sridhar Jul 02 '13 at 10:02
  • @BBdev could you tell me how to do that with ListView – Sridhar Jul 02 '13 at 10:02
  • @Sridhar in order to use ListViews in appwidgets take a look at http://developer.android.com/guide/topics/appwidgets/index.html#collections – Jose_GD Dec 18 '13 at 20:55

2 Answers2

0

Try to make Layout in XMl and set its value in Java file... and use method

          RemoteViews.setTextviewText(id,string);

try this method as i hv use it in my widget

        private class ABC extends TimerTask {
         RemoteViews remoteViews;
         AppWidgetManager appWidgetManager;
         ComponentName thisWidget;

         public ABC(Context context, final AppWidgetManager appWidgetManager) {
         this.appWidgetManager = appWidgetManager;


         remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
         thisWidget = new ComponentName(context, WidgetAnimate.class);
          }

         public void run() {

           String k= XMLfunctions.data1();   
           remoteViews.setTextViewText(R.id.tv12,k);
           appWidgetManager.updateAppWidget(thisWidget, remoteViews);

         }
0

Giving you a full code for that will be much bigger. I am giving you some code sample you can optimize it as you needed.

Android ListView example.

And this one is also a good tutorial to start with

Android ListView Tutorial

And if you want optimized your list you can see this tutorial.

ListView Tips & Tricks #3: Create Fancy ListViews

BBdev
  • 4,898
  • 2
  • 31
  • 45
  • Thanks but I asked you about how to set adapter in AppWidgetProvider class. – Sridhar Jul 02 '13 at 11:04
  • If you will use ListView you can directly set adapter to listview. The listview is a widget provided by Android and seeing what you are doing is good to achieve by the list view. – BBdev Jul 02 '13 at 11:06
  • how to get listview object – Sridhar Jul 02 '13 at 11:15
  • is RemoteView provide any special method like findViewByID() – Sridhar Jul 02 '13 at 11:16
  • Please refer to this answer [How to set custom ListAdapter to list view in appwidget?](http://stackoverflow.com/questions/14785446/how-to-set-custom-listadapter-to-list-view-in-appwidget) – BBdev Jul 02 '13 at 11:22