2016-05-11 8 views
0

私が間違っていることを理解するのに助けが必要です。私はArrayListを<db>に変換する<string>

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; 
} 

クラスShoppingListDbHelperを持っていると私はそう

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)でエラーが出ますArray<String>ArrayList<ShoppingListDbHelper>を使用してください。 ShoppingListDbHelperからStringに変更しようとしましたが、今度はslItem.setText(shoppinglist.SHOPPINGLIST_COLUMN_ITEM);のような項目にはアクセスできません。

ご協力いただければ幸いです。

+0

完全なアダプタコードを投稿できます! – varunkr

+0

完了。私はそれをArrayList を拡張するように変更しようとしましたが、それは要素にアクセスできないときです。 –

+0

正確なエラーを表示することはできますか? – varunkr

答えて

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; 
} 

happpCoding;

関連する問題