Question

I make an app in which phone contact show in listview. but i want to convert it into widget. ie, I want to show contact list on the homescreen in a listview. I searched on google and found some example but none worked. any one has any link. I am not posting my code because it not worked. any help will be appriciated

Update :-

I am able to show list on homescreen. how to bind this listview to contact list I tried to add this method in my viewfactory class

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);
    }

But it doesn't work, it is working in my app but not in homescreen widget

Update 2 :-

I am showing contact list in widget but all contacts are showing in one row.My remote view class is

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;
  } 
}
}
Was it helpful?

Solution

What you are looking for is AppWidgets using Collections. This can accept a ListView as the widget for presenting your data.

This will basically consist of

Since you are not sharing your code, it's hard to say what is not working for you, but you can find working examples in the sdk samples. Note that the samples have apparently been removed from latest sdk versions, so you should probably download the samples for older sdk's, if I remember correctly. You can also browse the source here.

EDIT :

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

This will not work in app-widgets, for 2 reasons :

  • You don't have an Activity available. For widgets, you can use the Context provided as argument of the onUpdate callback in your AppWidgetProvider, although in your case it probably won't be needed.

  • You don't instantiate an Adapter for app-widgets. To bind data to your ListView on your homescreen, you call setRemoteAdapter on the main RemoteView inside your AppWidgetProvider (in the onUpdate method), like this :

Example :

// 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);

Basically, the RemoteViewsFactory's getViewAt method acts much like the getView in an Adapter would, you just have to copy and adapt the code of your adapter to make it work in the RemoteViewsFactory. The sample of StackWidget contains a RemoteViewsFactory where all the method are commented to let you know where to perform which tasks.

EDIT 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();
}

Also, in you getViewAt method you are setting the text as the toString() of the array (this is why you have all contacts in one row), whereas you should read the fields of the individual Info object for the position given by the method:

// 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top