【问题标题】:copy specific item from one arraylist to another arraylist将特定项目从一个数组列表复制到另一个数组列表
【发布时间】:2011-06-24 01:58:18
【问题描述】:

我是 android progmmaing 的新手,我想问一个简单的问题

我已经成功地解析了一个 rss 提要,并将特定元素(例如标题、发布日期、链接、媒体和描述)保存在数据库中。然后我使用数组列表从数据库中检索数据。代码是

  public static  ArrayList<Item> GetItems(AndroidDB androiddb) {
   SQLiteDatabase DB = androiddb.getReadableDatabase();
    ArrayList<Item> result = new ArrayList<Item>();
    try {    
    Cursor c = DB.rawQuery("select * from ITEMS_TABLE", null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        do {
            result.add(new Item(
                    c.getString(0),
                    c.getString(1),
                    c.getString(2),
                    c.getString(3),
                    c.getString(4)));
        } while (c.moveToNext());

    } 
  c.close();
  DB.close();
} catch (SQLException e){
    Log.e("DATABASE", "Parsing Error", e);

}
return result;

}

其中 0 包含标题元素的数据库的第一列

现在我想只使用 title 元素创建一个列表视图,所以我在 onCreate 方法中创建了一个 ArrayList,我的问题是如何从以前的 ArrayList 中仅复制引用 Title 元素的项目。我已经写了这部分代码。我应该在循环中写什么来复制特定的项目?

      ArrayList<String> first_item = new ArrayList<String>();
                 items=AndroidDB.GetItems(rssHandler.androiddb);
                 int numRows=items.size();
                    for(int i=0; i < numRows; ++i)  {

                first_item.add());
                            }

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, first_item));

                  ListView lv = getListView();
                  lv.setTextFilterEnabled(true);

                  lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                      // When clicked, show a toast with the TextView text
                      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                          Toast.LENGTH_SHORT).show();
                    }
                  });
                } 

        catch (Exception e) {
            tv.setText("Error: " + e.getMessage());
            Log.e(MY_DEBUG_TAG, "Parsing Error", e);
            } 
        this.setContentView(tv);
    }

提前致谢

【问题讨论】:

    标签: java android arraylist


    【解决方案1】:

    几个快速的 cmets - 首先,

    if (c.getCount() > 0) {
        c.moveToFirst();
        do {
            result.add(new Item(
                    c.getString(0),
                    c.getString(1),
                    c.getString(2),
                    c.getString(3),
                    c.getString(4)));
        } while (c.moveToNext());
    }
    

    可以安全地替换为简单的:

    while (c.moveToNext()) {
        ....
    }
    

    以这种方式检查大小没有特别的原因,您不需要在光标上调用 moveToFirst()。这只是对可维护性的建议,并没有回答你的问题,但我想把它扔在那里以节省你将来的击键次数。

    就您的问题而言-如果我的理解正确,您希望从对象的复合列表中获取元素列表-基本上,一个列表由包含该属性的对象列表中特定属性的所有实例组成财产。没有捷径可以做到这一点。幸运的是,您可以比其他代码更干净地执行此操作:

    List<CompoundObjectWithAStringProperty> foo = /* go get the list */
    List<String> allProperties = new ArrayList<String>();
    
    for (CompoundObjectWithAStringProperty obj : foo) {
        allProperties.add(obj.getStringProperty());
    }
    

    您的代码已经完成了 90% 的工作,但它非常类似于 C。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多