我正在为记事本制作一个小部件,我能够从数据库中获取笔记的标题,但现在唯一的问题是我想要更改小部件的布局,即布局中的文本。我有一个列表视图我的配置活动,当我点击一个项目时,该项目中的文本应该成为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>
有人可以查看我的代码和帮助
答案 0 :(得分:1)
首先,我找不到你的AppWidgetProvider
课程。
其次在小部件中,您无法使用findViewById()
获取视图。在小部件中,您可以获得TextView
的视图,如下所示:
您必须在widget
RemoteViews
onUpdate()
AppWidgetProvider
中获取 RemoteViews rvs = new RemoteViews(context.getPackageName(), R.layout.main);
的布局:
TextView
现在在小部件的rvs.setTextViewText(R.id.textViewWidget, "we updated! yay!");
中设置文字:
{{1}}
如果您有任何进一步的疑问,请在widget上查看这个非常简单有效的教程。