具有基于用户登录的购物车片段

时间:2019-05-23 01:00:32

标签: android android-fragments android-sqlite session-management

我在此问题上停留了2天,我拥有保存购物车的sqlite数据库,我想在我打开购物车片段以仅显示由登录用户保存的订单时,我只尝试了Cursor for user_id,但未显示结果,因此我尝试列出<> getCart()并添加了游标,但它保留了以下错误消息:数组列表无法强制转换游标,即时消息字面性超出了选项,或者该怎么办,我尝试了所有操作,但找不到在线有用的东西


数据库

  public List<CartModelClass> getCarts() {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"ID", "user_id", "Food_id", "quantity", "price", "origin", "destination", "description","company_name","search_id"};
    String sqlTabel = "OrderDetails";

    qb.setTables(sqlTabel);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null,null);

    final List<CartModelClass> result = new ArrayList<>();
    if (c.moveToFirst()) {
        do {
            result.add(new CartModelClass(
                    c.getString(c.getColumnIndex("user_id")),
                    c.getString(c.getColumnIndex("Food_id")),
                    c.getString(c.getColumnIndex("quantity")),
                    c.getString(c.getColumnIndex("price")),
                    c.getString(c.getColumnIndex("origin")),
                    c.getString(c.getColumnIndex("destination")),
                    c.getString(c.getColumnIndex("description")),
                    c.getString(c.getColumnIndex("company_name")),
                    c.getString(c.getColumnIndex("search_id"))
            ));
        } while (c.moveToNext());
    }
    return result;
}

会话管理器

public class SessionManager {


SharedPreferences sharedPreferences;
public SharedPreferences.Editor editor;
public Context context;
int PRIVATE_MODE = 0;

private static final String PREF_NAME = "LOGIN";
private static final String LOGIN = "IS_LOGIN";
private static final String NAME = "NAME";
private static final String PHONE_NUM = "PHONE_NUM";
private static final String EMAIL = "EMAIL";
public static final String USER_ID = "USER_ID";


public SessionManager(Context context) {
    this.context = context;
    sharedPreferences = context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);
    editor =sharedPreferences.edit();
}

public void createSession(String name, String phone_num, String email , String user_id){

    editor.putBoolean(LOGIN, true);
    editor.putString(NAME, name);
    editor.putString(PHONE_NUM, phone_num);
    editor.putString(EMAIL, email);
    editor.putString(USER_ID, user_id);
    editor.apply();

}

public HashMap<String, String> getUserDetail(){

    HashMap<String, String> user = new HashMap<>();
    user.put(NAME, sharedPreferences.getString(NAME,null));
    user.put(PHONE_NUM, sharedPreferences.getString(PHONE_NUM,null));
    user.put(EMAIL, sharedPreferences.getString(EMAIL,null));
    user.put(USER_ID, sharedPreferences.getString(USER_ID,null));

    return user ;
}

public void logout(){

    editor.clear();
    editor.commit();
    Intent i = new Intent(context, LoginActivity.class);

}

}

购物车loadinglistfood

private void loadListFood() {


        listdata = new Database(this.getContext()).getCarts();


        adapter = new CartAdapter(listdata, this.getContext());
        recyclerView.setAdapter(adapter);


        int total = 0;
        for (CartModelClass order : listdata)

            total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));
        Locale locale = new Locale("en", "US");

        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        txtTotalPrice.setText(fmt.format(total));

    }


}

0 个答案:

没有答案