2016-05-11 16 views
0

Ich brauche Hilfe zu verstehen, was ich falsch mache. Ich habe eine Klasse ShoppingListDbHelperKonvertieren einer ArrayList <db> To ArrayList <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; 
} 

Und ich erstellt einen Adapter wie so

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

Und wenn ich versuche, diese

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

ich in ShoppingListAdapter adapter = new ShoppingListAdapter(context, shoppinglist) einen Fehler zu machen, die grundsätzlich sagt Verwenden Sie ArrayList<ShoppingListDbHelper> in Array<String> nicht. Ich habe versucht, von ShoppingListDbHelper zu String zu wechseln, aber dann kann ich nicht mehr auf die Artikel slItem.setText(shoppinglist.SHOPPINGLIST_COLUMN_ITEM); zugreifen.

Jede Hilfe wäre willkommen.

+0

Können Sie den vollständigen Adaptercode eingeben !! – varunkr

+0

Fertig. Ich habe versucht, es zu ändern, ArrayList zu erweitern, aber das ist, wenn ich nicht auf die Elemente zugreifen kann. –

+0

Kannst du den genauen Fehler angeben, den du bekommst? – varunkr

Antwort

0

Hoffe, Sie verstanden @varunkr Kommentare und unten ist die Änderungen, die Sie vornehmen müssen.

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;

Verwandte Themen