如何查看主屏幕小部件上的列表视图项?

时间:2014-01-29 04:34:30

标签: java android

我正在尝试创建一个包含元素列表的Android小部件,但是当在主屏幕上拖动小部件时,我会收到消息" loading"而不是我想要显示的项目的名称,有什么问题? 这是我的清单提供者:

package com.example.noteskeeper;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;
import android.appwidget.AppWidgetManager;

public class ListProvider implements RemoteViewsFactory {

    private Context context;
    private int appWidgetId;

    //private static final int mCount = 10;

    //Context context
    //private DBTools dbTools = new DBTools(context);
    ArrayList<HashMap<String,String>> lists;
    public ListProvider( Context context, Intent intent,ArrayList<HashMap<String,String>> listItems ){

        this.context = context;
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);

         lists = listItems;



    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lists.size();
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return Long.parseLong(lists.get(position).get("noteId"));
    }

    @Override
    public RemoteViews getLoadingView() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        // TODO Auto-generated method stub

         RemoteViews remoteView = new RemoteViews(
                context.getPackageName(), R.layout.note_entry);
        //HashMap<String,String> row = lists.get(position);
        remoteView.setTextViewText(R.id.tvNoteId, lists.get(position).get("noteId"));

        remoteView.setTextViewText(R.id.tvNoteTitle, lists.get(position).get("noteTitle"));
        return remoteView;
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDataSetChanged() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub

    }

}

这是我的服务:

package com.example.noteskeeper;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

public class NotesWidgetService extends RemoteViewsService {

    DBTools dbTools = new DBTools(this);
    @Override

    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        // TODO Auto-generated method stub
        return (new ListProvider(this.getApplicationContext(), intent,
                dbTools.getNotes()));
    }

}

这是我的提供者:

package com.example.noteskeeper;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.ListAdapter;
import android.widget.RemoteViews;
import android.widget.SimpleAdapter;

public class NotesAppWidgetProvider extends AppWidgetProvider {




    @SuppressWarnings("deprecation")
    @Override

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        final int N = appWidgetIds.length;
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.note_widget);

        for (int i=0; i<N; i++) {



            Intent intent = new Intent(context, NotesEditor.class);
            Intent intent2 = new Intent(context, NotesFirst.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);

            views.setOnClickPendingIntent(R.id.btn_wnew, pendingIntent);
            views.setOnClickPendingIntent(R.id.textView1, pendingIntent2);

            Intent svcIntent = new Intent(context, NotesWidgetService.class);
            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            svcIntent.setData(Uri.parse(
                    svcIntent.toUri(Intent.URI_INTENT_SCHEME)));





            views.setRemoteAdapter(appWidgetIds[i],R.id.noteslist,
                    svcIntent);

            views.setEmptyView(R.id.noteslist, R.id.empty_view);
            appWidgetManager.updateAppWidget(appWidgetIds[i], views);

        }




    }


}

我的xml文件: 小部件提供者:

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
         android:minWidth="250dip"
          android:minHeight="110dip"
           android:widgetCategory="home_screen|keyguard"
            android:initialLayout="@layout/note_widget" 
            android:resizeMode="horizontal|vertical" android:updatePeriodMillis="1800000">


    </appwidget-provider>

widget xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dp"

            android:background="@color/white">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:textColor="@color/white"
                android:background="@color/black"
                android:textAlignment="center"
                android:gravity="center"
                android:layout_marginRight="0.1dp"
                />

            <Button
                android:id="@+id/btn_wnew"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/as"
                android:background="@color/black"
                android:textSize="26sp"
                style="?android:attr/borderlessButtonStyle"
                android:textColor="@color/white"
                 />

            <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/empty_string"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:visibility="gone" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/list" >

            <ListView
            android:id="@+id/noteslist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Medium"
              >
        </ListView>
        </LinearLayout>



    </LinearLayout>

</FrameLayout>

我的元素xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvNoteId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tvNoteTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
             />

    </TableRow>

</TableLayout>

0 个答案:

没有答案