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);
のような項目にはアクセスできません。
ご協力いただければ幸いです。
完全なアダプタコードを投稿できます! – varunkr
完了。私はそれをArrayListを拡張するように変更しようとしましたが、それは要素にアクセスできないときです。 –
正確なエラーを表示することはできますか? – varunkr