开发者

copy specific item from one arraylist to another arraylist

im a newbie at android progmmaing and i want to ask a simple question

i have managed to parse an rss feed and to save specific elements (such as Title , pubdate, link , media and description) in a database. then i used an arraylist to retrieve the data from the database. 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

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. 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜