Есть фрагмент корзины на основе пользователя, зарегистрированного - PullRequest
0 голосов
/ 23 мая 2019

я застрял на этом в течение 2 дней, у меня есть база данных sqlite с сохраненной корзиной, и я хочу, чтобы при открытии фрагмента корзины отображался только заказ, сохраненный пользователем, вошедшим в систему, я пробовал Курсор только для 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);

}

}

Корзина loadlistfood

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

    }


}
...