Кнопка Удалить удаляет последний элемент на sqlite (android studio) - PullRequest
0 голосов
/ 16 марта 2020

У меня есть кнопка в моем обзоре утилит, называемая удалить. Я хочу удалить элемент из базы данных sqlite, если кнопка нажата, но вместо этого он удаляет последний элемент в таблице. Я проверил очень много сайтов, но все, что я вижу, это слайд, чтобы удалить, или долгое нажатие, чтобы удалить. Как мне добиться этого

Мой адаптер

public class SelectedProblemsAdapter extends RecyclerView.Adapter<SelectedProblemsAdapter.SelectedProblemsViewHolder> {

private List<SelectedProblemsModel> selectedProblems;
public static String price;
public static String problems;
Context context;
LikelyProblemAdapter.RecyclerViewClickListener mRecyclerViewClick;
Cursor cursor;
private String id;
public int problemsIndex, priceIndex, problem_id;
private String formatted_price;
SQLiteDatabase sqLiteDatabase;
OnItemClick listener;
public SelectedProblemsAdapter(Context context, Cursor cursor) {
    this.context = context;
    this.cursor = cursor;

}

@NonNull
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
    return new SelectedProblemsViewHolder(view);
}

Основная активность

public class SelectedProblems extends AppCompatActivity {
public static final int PROBLEMS = 0;
public static final int PRICE = 1;
private static final String[] SELECTED_PROBLEMS = {
        TranxavContract.CartEntries.PROBLEMS,
        TranxavContract.CartEntries.PRICE
};
public static final String FEED_BACK_COMPLETE_URL = MYURL.url + "feedbacks";
private static final int SELECTED_PROBLEMS_LOADER_ID = 0;
private RecyclerView mRecyclerView;
private double priceProblem;
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Button payForSelectedProblems;
List<SelectedProblemsModel> selectedProblemsModelList;
SelectedProblemsAdapter selectedProblemsAdapter;
TextView problemSelectedTotal;
int total = 0;
JSONObject jsonObject;
JSONArray jsonArray;
private String st;
private String selectedProblems;
private ProgressDialog progressDialog;
private final String URL = MYURL.url + "pay-from-our-server";
SQLiteDatabase mDatabase;
CartDbHelper cartDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selecte_problem_drawal);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));

    Helper helpers = new Helper();
    helpers.driverHeaderName(getApplicationContext(), this);

    cartDbHelper = new CartDbHelper(this);
    mDatabase = cartDbHelper.getWritableDatabase();

    problemSelectedTotal = findViewById(R.id.selectedProblemTotal);
    drawerLayout = (DrawerLayout) findViewById(R.id.selected_problem_drawal_layout);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    Toast.makeText(getApplicationContext(), selectedProblems, Toast.LENGTH_SHORT).show();

    final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer,R.string.close_drawer);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    System.out.println("My of Objects "+ st);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch (id){
                case R.id.dashoard:
                    Intent intent = new Intent(SelectedProblems.this, MainDashboard.class);
                    startActivity(intent);
                break;
                case R.id.hire_mechanic_id:
                    Intent intents = new Intent(SelectedProblems.this, LIkelyProblemsActivity.class);
                    startActivity(intents);
                break;
            }
            return true;
        }
    });
    mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems());
    mRecyclerView.setAdapter(selectedProblemsAdapter);

    Cursor cursor = mDatabase.query(
            CartContract.CartEntry.TABLE_NAME,
            null,
            null,
            null,
            null,
            null,
            null,
            null
    );

    jsonArray = new JSONArray();
    if(cursor!=null && cursor.getCount() > 0) {
        while (cursor.moveToNext()){
            jsonObject = new JSONObject();
            String selectedProblemPrice = cursor.getString(cursor.getColumnIndex(CartContract.CartEntry.PRICE));
            total = total + Integer.parseInt(cursor.getString(cursor.getColumnIndex(CartContract.CartEntry.PRICE)));
            selectedProblems = cursor.getString(cursor.getColumnIndex(CartContract.CartEntry._ID));
            try {
                jsonObject.put("problems", selectedProblems);
                jsonObject.put("price", selectedProblemPrice);
                jsonArray.put(jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        Preferences.setDefaults("selectedProblemsData", jsonArray.toString(), getApplicationContext());
        problemSelectedTotal.setText(String.valueOf(total));
    }
}

private Cursor getAllItems(){
    return  mDatabase.query(
            CartContract.CartEntry.TABLE_NAME,
            null,
            null,
            null,
            null,
            null,
            null,
            null
    );
}

}

...