私はこれを理解することができません、私はそれと何日も苦労してきました、そして私はそれにとてもうんざりしています。
構成アクティビティからスピナーを使用してAppWidgetのTextViewのテキストサイズを変更していますが、正しく更新できません。初めて指示したときは更新されませんが、2回目と3回目以降は毎回更新されます。
誰かが私のコードを見てくれたら本当にありがたいです
どうもありがとうございます
構成クラス:
public class WidgetConfig extends Activity implements OnItemSelectedListener{
Dialog myDialog;
Context context = WidgetConfig.this;
static EditText info;
static Spinner spinner;
String appwidget_textsize = "0";
private static final String[] paths = { "10", "12", "14", "16", "18", "20",
"22", "24", "26", "28", "30", "32", "34", "36", "38", "40", "50", "60"};
private static final String PREFS_NAME = "com.harteg.NotesWidgetPro.Widget";
private static final String PREF_PREFIX_KEY = "prefix_";
private static final String TAG = "MyActivity";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
float number;
public WidgetConfig() {
super();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate() started");
setContentView(R.layout.widgetconfig);
context = WidgetConfig.this;
// back button = cancel
setResult(RESULT_CANCELED);
info = (EditText) findViewById(R.id.etwidgetconfig);
findViewById(R.id.bwidgetconfig).setOnClickListener(mOnClickListener);
findViewById(R.id.bwidgetconfig1).setOnClickListener(mOnClickListener);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
info.setText(loadTitlePref(WidgetConfig.this, mAppWidgetId));
//------------ Text Size spinner ---------------
spinner = (Spinner) findViewById(R.id.TxtSizeSP);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(WidgetConfig.this,
android.R.layout.simple_spinner_item, paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//--------------------------------------------------
} // onCreate finished
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Log.v(TAG, "OnItemselected started");
switch (position) {
case 0:
info.setTextSize(10.0f);
Log.v(TAG, "position 0 chosed");
appwidget_textsize = "10".toString();
break;
...
}
number = Float.valueOf(appwidget_textsize);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "set remote view");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
Log.v(TAG, "set txt size");
views.setFloat(R.id.tvConfigInput, "setTextSize", number);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Log.v(TAG, "over txt size");
// When the button is clicked, save the string in our prefs and
// return that they clicked OK.
String titlePrefix = info.getText().toString();
saveTitlePref(context, mAppWidgetId, titlePrefix);
// Push widget update to surface with newly set prefix
Widget.updateAppWidget(context, appWidgetManager, mAppWidgetId,
titlePrefix, views);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);
setResult(RESULT_OK, resultValue);
Log.v(TAG, "onClick done");
finish();
}
};
// Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int mAppWidgetId, String text) {
SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
editor.putString(PREF_PREFIX_KEY + mAppWidgetId, text);
// Saves the values
editor.commit();
}
// Read the prefix from the SharedPreferences object for this widget.
static String loadTitlePref(Context context, int mAppWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String prefix = prefs.getString(PREF_PREFIX_KEY + mAppWidgetId, null);
// If there is no preference saved, get the default from a resource
if (prefix != null) {
return prefix;
} else {
return context.getString(R.string.appwidget_prefix_default);
}
}
static void deleteTitlePref(Context context, int mAppWidgetId) {
}
static void loadAllTitlePrefs(Context context,
ArrayList<Integer> mAppWidgetIds, ArrayList<String> texts) {
}
}
AppWidgetProviderクラス:
public class Widget extends AppWidgetProvider {
private static final String TAG = "MyActivity";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.v(TAG, "onUpdate started");
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int i=0; i<N; i++){
int mAppWidgetId = appWidgetIds[i];
String titlePrefix = WidgetConfig.loadTitlePref(context, mAppWidgetId);
updateAppWidget(context, appWidgetManager, mAppWidgetId, titlePrefix, views);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
WidgetConfig.deleteTitlePref(context, appWidgetIds[i]);
}
}
...
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int mAppWidgetId, String titlePrefix, RemoteViews views) {
Log.v(TAG, "updateAppWidget started");
// Getting the string this way allows the string to be localized. The format
// string is filled in using java.util.Formatter-style format strings.
CharSequence text = context.getString(R.string.appwidget_text_format,
WidgetConfig.loadTitlePref(context, mAppWidgetId));
appWidgetManager = AppWidgetManager.getInstance(context);
...
views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.tvConfigInput, text);
// Tell the AppWidgetManager to perform an update on the current app widget
Log.v(TAG, "Bottom update started");
appWidgetManager.updateAppWidget(mAppWidgetId, views);
}
}
Strings.xml
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="appwidget_prefix_default"></string>
<string name="appwidget_textsize"></string>
<string name="appwidget_text_format"><xliff:g id="prefix">%1$s</xliff:g></string>
...
</resources>