【发布时间】:2014-03-21 03:29:44
【问题描述】:
我制作了一个应用程序,其中电话联系人显示在列表视图中。但我想把它转换成小部件。即,我想在列表视图的主屏幕上显示联系人列表。我在谷歌上搜索并找到了一些示例,但没有一个有效。任何人都有任何链接。我没有发布我的代码,因为它不起作用。任何帮助都会得到帮助
更新:-
我可以在主屏幕上显示列表。 如何将此列表视图绑定到联系人列表 我试图在我的 viewfactory 类中添加这个方法
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
ids = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
aa.add(new ContactStock(info.name,info.phone));
}
phone.close();
//Collections.sort(aa);
adapt=new ContactListAdapter(MainActivity.this, aa);
//listcontact.setAdapter(new ContactListAdapter(MainActivity.this, aa));
//adapter = new ArrayAdapter<Info>(this, android.R.layout.simple_list_item_1);
listcontact.setAdapter(adapt);
}
但它不起作用,它在我的应用程序中工作,但不在主屏幕小部件中
更新 2:-
我在小部件中显示联系人列表,但所有联系人都显示在一行中。我的远程视图类是
public class DialerViewFactory implements RemoteViewsFactory {
private static final int mCount = 2;
private List<Info> mWidgetItems = new ArrayList<Info>();
private Context mContext;
private int mAppWidgetId;
public DialerViewFactory(Context context,Intent intent){
mContext=context;
mAppWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
@Override
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
//ContentResolver cr=mContext.getContentResolver();
//getnumber(cr);
RemoteViews rv=new RemoteViews(mContext.getPackageName(),R.layout.row);
rv.setTextViewText(R.id.textrow1, mWidgetItems.toString());
rv.setTextViewText(R.id.textrow2, "Kya");
Bundle extras = new Bundle();
extras.putInt(DialerWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.textrow1, fillInIntent);
return rv;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
ContentResolver cr1=mContext.getContentResolver();
getnumber(cr1);
}
@Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
}
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
mWidgetItems.add(info);
}
phone.close();
}
class Info {
public String name;
public String phone;
public String picture;
@Override
public String toString() {
return name;
}
}
}
【问题讨论】:
标签: android listview android-widget