我正在为记事本制作一个小部件,我可以从数据库中获取笔记的标题,但现在唯一的问题是我想更改小部件的布局,即布局中的文本。我的配置活动中有一个列表视图当我单击一个项目时,该项目中的文本应该成为 Widget 中 textView 的文本。现在我能够获取文本数据,但我无法设置它。这是小部件提供程序的代码:-
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>
有人可以看看我的代码并提供帮助吗