2012-05-04 15 views
0

イメージボタンとテキストビューでウィジェットを作成しようとしています。イメージボタンを押すと、テキストを配列からランダムな文字列に変更します。私は仕事ができると思うものを持っているが、テキストを更新していないので、理由が分からない。私のイメージボタンはimagewidgetericaと呼ばれ、私のtextviewはwidget1labelと呼ばれます。ウィジェットがクリックを登録しないように見える

HERESに私のコード:

package kevin.erica.box; 

import java.util.Arrays; 
import java.util.Random; 

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.RemoteViews; 
import android.widget.TextView; 



public class Widget extends AppWidgetProvider { 




private static final Random rgenerator = new Random(); 
private static final Random rgenerator2 = new Random(); 
private String[] myString1; 






    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     Resources res = context.getResources(); 
     myString1 = res.getStringArray(R.array.myArray); 


    final int N = appWidgetIds.length; 
    Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds)); 
    // Perform this loop procedure for each App Widget that belongs to this 
    // provider 
    for (int i = 0; i < N; i++) { 
     int appWidgetId = appWidgetIds[i]; 
     // Create an Intent to launch ExampleActivity 
     Intent intent = new Intent(context, Widget.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     // Get the layout for the App Widget and attach an on-click listener 
     // to the button 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
     views.setOnClickPendingIntent(R.id.imagewidgeterica, pendingIntent); 
     // To update a label 
     views.setTextViewText((R.id.widget1label, myString1[rgenerator.nextInt(myString1.length)]); 
     // Tell the AppWidgetManager to perform an update on the current app 
     // widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
    } 
} 

マニフェスト:

<receiver android:name=".Widget" android:label="Erica's Box"> 
    <intent-filter> 
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
<meta-data android:name="android.appwidget.provider" android:resource="@xml/erica_info" /> 
</receiver> 

答えて

0
import java.util.Random; 

import android.app.Activity; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class TestActivity extends Activity { 
/** Called when the activity is first created. */ 

private String[] myString; 
private static final Random rgenerator = new Random(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

final String[] myString; 

Resources res = getResources(); 

myString = res.getStringArray(R.array.myArray); 

Button generate = (Button) findViewById(R.id.generate); 


generate.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
    String q = myString[rgenerator.nextInt(myString.length)]; 
    TextView tv = (TextView) findViewById(R.id.textview); 
    tv.setText(q);}}); 
} 
} 

/res/values/array.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string-array name="myArray"> 
<item>string 1</item> 
<item>string 2</item> 
<item>string 3</item> 
<item>string 4</item> 
<item>string 5</item> 
</string-array> 

</resources> 
+0

これは私が働いていたものです – Storm2010

関連する問題