2013-02-15 76 views
0

我正在做一個記事本的小部件,我能夠從數據庫中抓取筆記的標題,但現在唯一的問題是我想改變小部件的佈局,即文本在layout.I有一個ListView在我的配置活動,當我點擊一個項目在該項目的文本應該成爲的TextView的Widget.Now我能夠得到的文本數據的文字,但我不能設置它。 繼承人的微件提供者的代碼: -遠程textview沒有改變小部件配置活動android

public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 
    // TODO Auto-generated method stub 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
    for (int i = 0; i < appWidgetIds.length; i++) { 
     int appWidgetId = appWidgetIds[i]; 

     Intent intent = new Intent(context, NotesList.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       intent, 0); 

     RemoteViews views = new RemoteViews(context.getPackageName(), 
       R.layout.widget_layout); 
     views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); 

     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
} 

和配置部件: -

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    if (intent.getData() == null) { 
     intent.setData(Notes.CONTENT_URI); 
    } 

    Uri notesUri = getIntent().getData(); 
    Cursor managedCursor = getContentResolver().query(notesUri, 
      new String[] { Notes.TAGS, Notes.TITLE }, null, null, null); 

    managedCursor.moveToFirst(); 
    do{ 
     Log.d("name", managedCursor.getString(1)); 
     name = managedCursor.getString(1); 
     array.add(name); 
     managedCursor.moveToNext(); 
    }while(!managedCursor.isLast()); 
    name = managedCursor.getString(1); 
    array.add(name); 

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array); 
    setListAdapter(adapter); 

    setResult(RESULT_CANCELED); 
    Bundle extras = getIntent().getExtras(); 

    if(extras != null){ 
     widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 

    appWidgetManager = AppWidgetManager.getInstance(this); 
} 

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Uri notesUri = getIntent().getData(); 
    Cursor managedCursor = getContentResolver().query(notesUri, 
      null, null, null, null); 

    managedCursor.moveToFirst(); 
    managedCursor.moveToPosition(position); 
    Log.d("Note", managedCursor.getString(9)); 
    String s = managedCursor.getString(9); 
    //s is the text I want to set & here since I need to change a different layout so I used layout inflator 
    LayoutInflater inflate = getLayoutInflater(); 
    LinearLayout v1 = (LinearLayout) inflate.inflate(R.layout.widget_layout, null); 
    text = (TextView)v1.findViewById(R.id.textViewWidget); 
    //These are the changed needed to be done 
      text.setBackgroundColor(Color.YELLOW); 
    text.setText(s); 
    //Toast.makeText(this, text.getText()+"", Toast.LENGTH_SHORT).show(); 
    views = new RemoteViews(this.getPackageName(), 
      R.layout.widget_layout);//This remote view is not updated 

    Intent intent1 = new Intent(this, NotesList.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      intent1, 0); 
    views.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); 

    appWidgetManager.updateAppWidget(widgetId, views); 

    Intent resultValue = new Intent(); 
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); 
    setResult(RESULT_OK,resultValue); 
    finish(); 
} 

的配置活動的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:orientation="vertical" > 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fastScrollEnabled="true" > 
</ListView> 

</LinearLayout> 

和部件: -

<?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:orientation="vertical"> 

<TextView 
    android:id="@+id/textViewWidget" 
    android:layout_width="96dp" 
    android:layout_height="96dp" 
    android:background="@android:color/white" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="@android:color/black" /> 

</LinearLayout> 

可能有人請看看我的代碼,並幫助

+0

請同時發佈你的xml文件的小部件佈局。你的列表視圖在哪裏? – Rahil2952 2013-02-15 10:43:51

+0

針對widgetConfig文件的列表視圖以及小部件文件發佈的XML代碼。 – 2013-02-15 10:49:35

+0

土司onListItemClick做出不顯示我的TextView的變化值,但只要配置活動結束小部件顯示原始佈局。 – 2013-02-15 10:56:03

回答

1

首先我找不到你的AppWidgetProvider類。

其次在小部件,你不能得到使用findViewById()。在小部件,你可以得到鑑於TextView象下面這樣的觀點:

你有RemoteViews這樣,這也是在AppWidgetProvideronUpdate()獲取的widget佈局:

RemoteViews rvs = new RemoteViews(context.getPackageName(), R.layout.main); 

我們設置文本控件的TextView

rvs.setTextViewText(R.id.textViewWidget, "we updated! yay!"); 

如果您有任何進一步查詢,請只檢查widget這個非常簡單而有效的教程。

+0

@ hitman_93如果它對您有幫助,請將其標記爲答案。 – Rahil2952 2013-02-15 11:29:53

+0

好的謝謝你Rahil整理我的問題,但這裏來了一個新的我的Widget文本不更新時,我在應用程序本身更改我的筆記 – 2013-02-15 11:47:15

+0

我已經在小部件xml文件中提供更新後1000毫秒,所以技術上onupdate方法小部件提供者類應該在這段時間之後調用?所以現在從widgetId我怎麼知道哪個列表項在配置文件中被點擊了,所以我應該更新提供者類 – 2013-02-15 11:56:36

相關問題