• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

multiple listviews or sql

branedge

Lurker
Feb 12, 2018
3
0
I'm working on a car maintenance app run into little problem. right now i have the user add his car(s) to my app which populates a listview in the mainactivity. then when the user clicks one of the cars it will go to another activity to add (for example) and oil change then this "oil change" will be listed under that car. the problem is all the "maintenance entries" show under all the cars. it supposed to open up with the car and only show the items for that car. right now i'm using 2 separate arraylist but i'm not sure how to tie them together or filter them..or do i need to use sql?

thanks
 
this is from my mainactivity this populates my list and set the onItemClickListener

Java:
    //this populates the list view
    @Override
   protected void onResume() {
        super.onResume();
        main_car_list.setAdapter(null);
        cars = Utilities.getAllSavedCars(this);
   //for context menu
        registerForContextMenu(main_car_list);

        if(cars == null || cars.size() == 0){
            Toast.makeText(this,"you have no cars added", Toast.LENGTH_SHORT).show();
            return;
        }else{
            //removed CarAdapter ca and moved CarAdapter up top so could use to delete items
            ca = new CarAdapter(this, R.layout.car_list, cars); //from caradapter
            main_car_list.setAdapter(ca);

   //when click item(car) opens new activity
            main_car_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { //tabbed automatically filled in this
                    String fileName = ((Cars)main_car_list.getItemAtPosition(position)).getDateTime() + Utilities.FILE_EXTENSION; //gets filename

                    Intent viewCar = new Intent(getApplicationContext(),CarDetails.class); //when clicked on item in list opens deails
                    //this is how pass data between activities

                  //   this passes car name and model to CarDetails
                    viewCar.putExtra("Car_File", fileName);  //Car_File is a key (like naming the file)

                    startActivity(viewCar);


                }
            });

            }

        }

//for context menu (long press menu) have to add registerForContextMenu(main_car_list) in the above list view
//   @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.menu_deleteItem:
              //   Toast.makeText(this,"clicked"+ca.getItem(info.position), Toast.LENGTH_SHORT).show();
//works!!!!!!
                Utilities.deleteCar(getApplicationContext(), ((Cars)main_car_list.getItemAtPosition(info.position)).getDateTime() + Utilities.FILE_EXTENSION);
               //above line deletes the file, next 2 lines removes it from the adapter (makes it go away and refreshes the list view)
                cars.remove(info.position);
                ca.notifyDataSetChanged();
               return true;

            default:
                return super.onContextItemSelected(item);
        }
    }


this is where the car should be listed with only it's maintenance records listed
Java:
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_details);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        detailsMake = findViewById(R.id.detailsMake);
        detailsModel = findViewById(R.id.detailsModel);

        OilChangeRecords = findViewById(R.id.listOilChangeRecords);

//this is passing car name here
        carFileName = getIntent().getStringExtra("Car_File"); //supposed to get file created and tagged "Car_File"

        if(carFileName != null && !carFileName.isEmpty()){  //if not null and not empty (!carFileName - not)
            loadCar = Utilities.getCarByFileName(this,carFileName);


            if (loadCar != null ){
                detailsMake.setText(loadCar.getMake());
                detailsModel.setText(loadCar.getModel());
            }


        }


    }


not sure what you may need to see but i will be glad to share all i have. thanks
 
Upvote 0
Hi,
you can do any of both ways:
  1. with a data base and you will need at least two tables: one table CAR(car_description, id) and a new table MAINTENANCE(maintenance_description, car_id) , and you can have . In this way you link cars and maintenance operations
  2. In java data structures: a hash map where keys are car_description (type String), and the values are the maintenance operations stored as ArrayList of Strings.
If you need to store the data from one run to the other a database is recommended.
 
Upvote 0
Hi,
you can do any of both ways:
  1. with a data base and you will need at least two tables: one table CAR(car_description, id) and a new table MAINTENANCE(maintenance_description, car_id) , and you can have . In this way you link cars and maintenance operations
  2. In java data structures: a hash map where keys are car_description (type String), and the values are the maintenance operations stored as ArrayList of Strings.
If you need to store the data from one run to the other a database is recommended.

Thanks, i think it may be best if i go with multiple tables since the users will be entering the cars and the maintenance i a free form manner so that may make the 2nd option harder ...
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones