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.
Können Sie den vollständigen Adaptercode eingeben !! – varunkr
Fertig. Ich habe versucht, es zu ändern, ArrayList zu erweitern, aber das ist, wenn ich nicht auf die Elemente zugreifen kann. –
Kannst du den genauen Fehler angeben, den du bekommst? – varunkr