【问题标题】:Contacts are listing in one row of listview?联系人在列表视图的一行中列出?
【发布时间】: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


    【解决方案1】:

    您正在寻找的是AppWidgets using Collections。这可以接受ListView 作为展示数据的小部件。

    这基本上包括

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

    编辑

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

    这在应用小部件中不起作用,原因有两个:

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

    • 您不会为应用小部件实例化 Adapter。要将数据绑定到主屏幕上的 ListView,请在 AppWidgetProvider 内的主 RemoteView 上调用 setRemoteAdapter(在 onUpdate 方法中),如下所示:

    例子:

    // 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'sgetViewAt 方法的行为与Adapter 中的getView 非常相似,您只需复制并修改适配器的代码以使其在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);
    

    【讨论】:

    • 感谢分享链接,我处理它们并能够在主屏幕上创建列表视图。但如何在主屏幕上显示联系人列表。我尝试了一些远程视图工厂方法但不起作用。
    • 编辑了我的答案以显示如何将数据绑定到主屏幕上的 ListView。
    • 感谢编辑,我的列表视图以数组的形式在一行中显示所有联系人?
    • 再次编辑 ;) 不确定您打算如何处理 fillInIntent 中的位置,但如果小部件应该在单击一行时调用联系人,那么传递不是更好直接电话号码?另外,我不确定,但我认为最好为每个 ListView 行的父布局设置一个 ID,并为该 ID 设置 fillInIntent,而不是在行内的一个特定 TextView 上。我从来没有测试过,所以我可能错了,但在父布局上设置它绝对适合我;)
    • 谢谢你解决了我的问题,+1 + 为你接受 ans + 赏金,你能告诉我如果我可以在小部件中添加编辑文本并通过输入编辑文本执行搜索,我可以在应用程序中轻松完成。但是如果我将edittext添加到listview中的小部件布局,它会从主屏幕隐藏,它会显示问题加载小部件。如果可能的话,任何链接或提示如何做到这一点。
    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2015-05-31
    • 2014-07-12
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多