【问题标题】:how to add List to Array List in android?如何在android中将列表添加到数组列表?
【发布时间】:2015-03-26 13:55:13
【问题描述】:

我正在使用 TelephonyManager 获取 getNeighboringCellInfo()。我想在 listview 中列出相邻单元格的详细信息。我成功获取了相邻单元格,但无法在列表视图中显示 请帮助我来解决这个问题。

listedSignals = (ListView)findViewById(R.id.listSignals);

private void getNeighbors(TelephonyManager telephonyManager) throws InterruptedException {
   List<NeighboringCellInfo> neighboringCellInfos;
   neighboringCellInfos = telephonyManager.getNeighboringCellInfo();
   neighboringCellBlockingQueue = new LinkedBlockingQueue<>(100);
   for (int i = 0;i < neighboringCellInfos.size();i++) {
       NeighboringCellInfo info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
       ArrayList<NeighboringCellInfo> cellInfoList = new ArrayList<>(neighboringCellBlockingQueue.size() + 1);
       while (info != null) {
           cellInfoList.add(info);
           info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
       }
       neighboringCellInfos = cellInfoList;
   }
}

neighboringCellInfos 有所有相邻单元格的详细信息。所以请在listedSignalslistview 中使用neighboringCellInfos 告诉我。

【问题讨论】:

  • 首先你应该将ArrayList&lt;NeighboringCellInfo&gt; cellInfoList = new ArrayList&lt;&gt;(neighboringCellBlockingQueue.size() + 1);移出for循环,因为每次在for循环中都会再次创建它,将之前的附加值再次重置为空。

标签: java android telephony telephonymanager


【解决方案1】:

您需要创建一个adapter 特定于您要在ListView 中显示的信息(SO 上已经有many examples of this)。使用您自己的自定义适配器,您可以定义要在ListView 中显示的信息。完成后,您只需将列表添加到适配器并在您的活动中设置adapterListView,如下所示:

CustomListAdapter myAdapter = new CustomListAdapter(this, cellInfoList, ...);
listedSignals.setListAdapter(myAdapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多