将ArrayList <db>转换为ArrayList <string>

时间:2016-05-11 00:35:08

标签: android listview

我需要帮助了解我做错了什么。我有一个类ShoppingListDbHelper

include_directories("include")
include_directories("src")

set( proj_SOURCE
    src/a.cpp
)

我创建了一个像这样的适配器

a.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl A::metaObject(void)const
" (?metaObject@A@@UEBAPEBUQMetaObject@@XZ) [C:\Users\me\AppData\Local\Temp\subclass\build\proj.vcxproj]
a.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl A::qt_metacast(char const *)" (?qt_metacast@A
@@UEAAPEAXPEBD@Z) [C:\Users\me\AppData\Local\Temp\subclass\build\proj.vcxproj]
a.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl A::qt_metacall(enum QMetaObject::Call,int,void *
 *)" (?qt_metacall@A@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) [C:\Users\me\AppData\Local\Temp\subclass\build\proj.vcxproj]
C:\Users\me\AppData\Local\Temp\subclass\build\Debug\proj.exe : fatal error LNK1120: 3 unresolved externals [C:\Users\me\Ap
pData\Local\Temp\subclass\build\proj.vcxproj]

当我尝试这样做时

public ArrayList<String> getAllShoppingList() {
    ArrayList<String> array_list = new ArrayList<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + SHOPPINGLIST_TABLE_NAME, null);
    res.moveToFirst();

    while (res.isAfterLast() == false) {
        array_list.add(res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_ITEM)));
        array_list.add(res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_QTY)) + " "
                + res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_SIZE)));
        array_list.add(res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_BRAND)));
        array_list.add(res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_ID)));
        res.moveToNext();
    }
    return array_list;
}

我在public class ShoppingListAdapter extends ArrayAdapter<ShoppingListDBHelper> { public ShoppingListAdapter(Context context, ArrayList<ShoppingListDBHelper> shoppinglists) { super(context, 0, shoppinglists); } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position ShoppingListDBHelper shoppinglist = getItem(position); // Check if an existing view is being reused, otherwise inflate the view if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.simple_list_item_1, parent, false); } // Lookup view for data population TextView slItem = (TextView) convertView.findViewById(R.id.listViewShoppingListItem); TextView slQty = (TextView) convertView.findViewById(R.id.listViewShoppingListQty); slItem.setText(shoppinglist.SHOPPINGLIST_COLUMN_ITEM); slQty.setText(shoppinglist.SHOPPINGLIST_COLUMN_QTY); // Return the completed view to render on screen return convertView; } } 中收到的错误基本上表示protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shopping_list_view); Context context = getApplicationContext(); ShoppingListDBHelper shoppingList = new ShoppingListDBHelper(context); ArrayList<ShoppingListDBHelper> shoppinglist; shoppinglist = shoppingList.getAllShoppingList(); ListView listView = (ListView) findViewById(R.id.shoppingListView); ShoppingListAdapter adapter = new ShoppingListAdapter(context, shoppinglist); listView.setAdapter(adapter); } 中无法使用ShoppingListAdapter adapter = new ShoppingListAdapter(context, shoppinglist)。我尝试从ShoppingListDbHelper更改为String,但之后我无法访问此ArrayList<ShoppingListDbHelper>之类的项目。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

希望您了解@varunkr评论,以下是您需要做出的更改。

public ArrayList<ShoppingListDBHelper> getAllShoppingList() {
    ArrayList<ShoppingListDBHelper > array_list = new ArrayList<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + SHOPPINGLIST_TABLE_NAME, null);
    res.moveToFirst();

    while (res.isAfterLast() == false) {
        item=new ShoppingListDBHelper();//create one item

        item.SHOPPINGLIST_COLUMN_ITEM=res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_ITEM));
        item.SHOPPINGLIST_COLUMN_QTY=res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_QTY));
        //do the same other objects of ShoppingListDBHelper
        //item.SHOPPINGLIST_COLUMN_BRAND=res.getString(res.getColumnIndex(SHOPPINGLIST_COLUMN_BRAND));
        //add item to the arraylist
        array_list.add(item);
        res.moveToNext();
    }
    return array_list;
}