[英]copy specific item from one arraylist to another arraylist
im a newbie at android progmmaing and i want to ask a simple question 我是android progmmaing的新手,我想问一个简单的问题
i have managed to parse an rss feed and to save specific elements (such as Title , pubdate, link , media and description) in a database. 我已经设法解析一个RSS提要,并将特定的元素(例如Title,pubdate,link,media和description)保存在数据库中。 then i used an arraylist to retrieve the data from the database. 然后我使用了一个arraylist从数据库中检索数据。 the code for this is 该代码是
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;
} }
where 0 the first column of the database which contains the title element 其中0包含标题元素的数据库的第一列
now i want to create a listview only with the title element so i created a ArrayList in my onCreate method and my question is how can i copy from the previous ArrayList only the items that refers to the Title element. 现在我只想用title元素创建一个listview,所以我在onCreate方法中创建了一个ArrayList,我的问题是我如何才能从以前的ArrayList中仅复制引用Title元素的项目。 i have written this part of code. 我已经编写了这部分代码。 What i supposed to write in the loop to copy the specific item? 我应该在循环中写些什么来复制特定项目?
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);
}
thanks in advance 提前致谢
a couple of quick comments - firstly, 一些简短的评论-首先,
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());
}
can safely be replaced with a simple: 可以安全地替换为以下简单方法:
while (c.moveToNext()) {
....
}
There's no particular reason to check the size this way, and you don't need to call moveToFirst() on the cursor. 没有特别的理由以这种方式检查大小,并且您不需要在游标上调用moveToFirst()。 That's just a suggestion for maintainability, and doesn't answer your question but I wanted to throw it out there to save you keystrokes in the future. 这只是对可维护性的建议,并不能回答您的问题,但我想将其扔在那里,以节省将来的击键次数。
As far as your question - if I'm understanding correctly, you want to get a list of elements from a compound list of objects - basically, a list comprised of all instances of a particular property within a list of objects holding that property. 就您的问题-如果我理解正确,您想从复合对象列表中获取元素列表-基本上,该列表由包含该属性的对象列表中特定属性的所有实例组成。 There's no shortcut to do this. 没有捷径可做。 Luckily you can do this more cleanly than your other code: 幸运的是,您可以比其他代码更简洁地执行此操作:
List<CompoundObjectWithAStringProperty> foo = /* go get the list */
List<String> allProperties = new ArrayList<String>();
for (CompoundObjectWithAStringProperty obj : foo) {
allProperties.add(obj.getStringProperty());
}
Your code is 90% of the way there but its oh so C-like. 您的代码在那儿占了90%,但是它是如此的类似于C。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.