联系人列在listview的一行?

时间:2014-02-20 09:30:15

标签: android listview android-widget

我制作了一个应用程序,其中列表视图中显示了手机联系人。但我想将其转换为小部件。即,我想在列表视图中显示主屏幕上的联系人列表。我在谷歌上搜索并找到了一些例子,但都没有用。任何人都有任何链接。我没有发布我的代码,因为它没有用。任何帮助都将得到满足

更新: -

我可以在主屏幕上显示列表。 如何将此列表视图绑定到联系人列表 我试图在我的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;
  } 
}
}

1 个答案:

答案 0 :(得分:0)

您要找的是AppWidgets using Collections。这可以接受ListView作为呈现数据的小部件。

这基本上由

组成

由于您没有共享代码,因此很难说什么不适合您,但您可以在sdk示例中找到有用的示例。请注意,样本显然已从最新的sdk版本中删除,因此如果我没记错的话,你应该下载旧版sdk的样本。您还可以浏览来源here

编辑

adapt=new ContactListAdapter(MainActivity.this, aa);
listcontact.setAdapter(adapt);

这在app-widget中不起作用,原因有两个:

  • 您没有可用的活动。对于小部件,您可以使用在onUpdate中作为AppWidgetProvider回调的参数提供的Context,尽管在您的情况下可能不需要它。

  • 您没有为app-widgets实例化Adapter。要将数据绑定到主屏幕上的ListView,请在RemoteViewAppWidgetProvider方法)内的主onUpdate上致电setRemoteAdapter,如下所示:

示例:

// Setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

// Creating the main RemoteView and binding data to it.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rv.setRemoteAdapter(R.id.listview, intent);

基本上,RemoteViewsFactory's getViewAt方法与getView中的Adapter非常相似,您只需要复制并调整适配器的代码即可在RemoteViewsFactory工作。 StackWidget的示例包含RemoteViewsFactory,其中所有方法都被注释,以告知您执行哪些任务的位置。

编辑2:

@Override
public int getCount() {
    // This method tells the factory how many rows it has to produce
    // you should be returning the size of your data collection
    // so in your case :
    return mWidgetItems.size();
}

此外,在您getViewAt方法中,您将文本设置为数组的toString()(这就是为什么您将所有联系人放在一行中),而您应该阅读个人的字段该方法给出的位置的Info对象:

// get the current Info object by position :
Info currentInfo = mWidgetItems.get(position);
// then use that object to fill each row, like in getView for an Adapter, for example :
rv.setTextViewText(R.id.textrow1, currentInfo.phone);
rv.setTextViewText(R.id.textrow2, currentInfo.name);