2013-01-02 29 views
7

मैं तत्वों (कार्यों) की एक सूची युक्त एक एंड्रॉइड विजेट बनाने की कोशिश कर रहा हूं।एंड्रॉइड में होमस्क्रीन विजेट में ListView भरना

जब मैं होमस्क्रीन पर विजेट डालता हूं तो यह खाली होता है, कोई भी कार्य प्रदर्शित नहीं होता है।

मुझे यह नहीं पता कि क्या गुम है।

आपकी मदद के लिए अग्रिम धन्यवाद।

यहाँ मेरी widget_layout.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="match_parent" 
android:background="@drawable/widget_background" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/title" 
    style="@android:style/TextAppearance.Medium" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_margin="4dip" 
    android:gravity="center_horizontal" 
    android:text="@+string/TASK_LIST_ACT" > 
</TextView> 

<ListView 
    android:id="@+id/list" 
    style="@android:style/TextAppearance.Medium" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</ListView> 

इस ListView

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/widget_element" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" /> 

की मदों के लिए लेआउट यह मेरा widgetProvider वर्ग है

package it.geotask.android.widget; 

import it.geotask.android.R; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.widget.RemoteViews; 

public class TaskWidgetProvider extends AppWidgetProvider { 

public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 

    for (int i = 0; i < appWidgetIds.length; ++i) { 

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

     RemoteViews rv = new RemoteViews(context.getPackageName(), 
       R.layout.widget_layout); 

     rv.setRemoteAdapter(appWidgetIds[i], R.id.list, intent); 

     rv.setEmptyView(R.id.list, R.id.empty_view); 
     appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
    } 

} 
} 

और ये मेरे RemoteViewsFactory

package it.geotask.android.widget; 

import it.geotask.android.R; 
import it.geotask.android.dto.Task; 
import it.geotask.android.service.TaskService; 

import java.util.ArrayList; 
import java.util.List; 

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

public class GeoTaskRemoteViewsFactory implements 
    RemoteViewsService.RemoteViewsFactory { 

private Context context = null; 
private int appWidgetId; 

private List<Task> taskList = new ArrayList<Task>(); 

public GeoTaskRemoteViewsFactory(Context context, Intent intent) { 
    this.context = context; 
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
      AppWidgetManager.INVALID_APPWIDGET_ID); 


} 

public int getCount() { 
    return taskList.size(); 
} 

public long getItemId(int position) { 
    return taskList.get(position).getId(); 
} 

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

public RemoteViews getViewAt(int position) { 
    RemoteViews row = new RemoteViews(context.getPackageName(), 
      R.layout.row); 

    row.setTextViewText(android.R.id.text1, taskList.get(position) 
      .getName()); 


    return (row); 
} 

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

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

public void onCreate() { 
    taskList = TaskService.getTaskInStateList(context, context 
      .getResources().getString(R.string.DB_STATE_ACTIVE)); 
    taskList.addAll(TaskService.getTaskInStateList(context, context 
      .getResources().getString(R.string.DB_STATE_IN_RANGE))); 
    taskList.addAll(TaskService.getTaskInStateList(context, context 
      .getResources().getString(R.string.DB_STATE_DELAYED))); 

} 

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

} 

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

} 

} 

remoteViewsService बस कारखाने का एक उदाहरण देता है।

संपादित करें: मैं मैनिफेस्ट में सेवा घोषित करना भूल गया; अब मुझे आइटम की सही संख्या से भरा विजेट मिलता है, लेकिन आइटम के नाम के बजाय "लोडिंग" संदेश के साथ मैं

उत्तर

6

प्रदर्शित करना चाहता हूं। मैं stackWidget के Google उदाहरण का पालन कर रहा था, और वे विजेट Ids सरणी पर एक का उपयोग करते हैं, int कर्सर को ++ i के साथ बढ़ाते हुए, यह विजेट को काम नहीं करता है। प्रयुक्त i ++ और यह

+0

काम करता है मुझे एक ही समस्या है। मैं वस्तुओं की संख्या देख रहा हूं, लेकिन वे सभी कहते हैं "लोड हो रहा है ..." और मेरे पास i ++ के साथ लूप बढ़ने के लिए है। –

+0

@KrisB आपने अपनी समस्या का समाधान कैसे किया। मैं वही मिल रहा हूँ। – Payal