Reputation: 131
I made an app which displays some information about some products, I achieved this by putting the brands in an arraylist on my main activity, and then after the user selects a brand he can choose a specific model, I did this by making a global xml file, and a class for each model with Strings and ints as the details, now the thing is, there are potentially hundreds and even over thousand models, is there an easier way to achieve this? like putting ALL of the data in one class and just pull it from it? or would it make things messier?
just to let you guys know that i'm a total beginner.
This is my code:
main activity shows a list of brands:
package com.example.ofir.myapplication;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.ofir.myapplication.lists.bmwList;
import com.example.ofir.myapplication.lists.ktmList;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<Brands> brands = new ArrayList<>();
brands.add(new Brands("KTM", ContextCompat.getColor(this, R.color.colorOrange)));
brands.add(new Brands("BMW", ContextCompat.getColor(this, R.color.colorPrimary)));
brands.add(new Brands("Suzuki", ContextCompat.getColor(this, R.color.colorPrimary)));
BrandAdapter itemsAdapter = new BrandAdapter(this, brands);
ListView listView = (ListView) findViewById(R.id.brandlist);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
Intent intent = new Intent(MainActivity.this, ktmList.class);
startActivity(intent);
}else if (position == 1) {
Intent intent2 = new Intent(MainActivity.this, bmwList.class);
startActivity(intent2);
}
}
});
}
}
each brands opens up models list:
for testing purposes I just set 2 brands with lists.
package com.example.ofir.myapplication.lists;
import com.example.ofir.myapplication.R;
import com.example.ofir.myapplication.models.dukeSmall;
import com.example.ofir.myapplication.models.smc;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import static android.media.CamcorderProfile.get;
public class ktmList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.models_list);
final ArrayList<String> ktmlist = new ArrayList<String>();
ktmlist.add("Duke 125");
ktmlist.add("Duke 200");
ktmlist.add("Duke 390");
ktmlist.add("Duke 690");
ktmlist.add("RC 125");
ktmlist.add("RC 200");
ktmlist.add("RC 390");
ktmlist.add("RC8");
ktmlist.add("SMC-R 690");
ktmlist.add("SMR 990");
ktmlist.add("SuperDuke 990");
ktmlist.add("SuperDuke 1290");
final ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ktmlist);
ListView listView = (ListView) findViewById(R.id.models_list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
Intent intent = new Intent(ktmList.this, dukeSmall.class);
startActivity(intent);
}else if (position == 8){
Intent intent = new Intent(ktmList.this, smc.class);
startActivity(intent);
}
}
});
}
}
and then I have a model class for each model with its specification, an example for a model is:
package com.example.ofir.myapplication.models;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.ofir.myapplication.R;
/**
* Created by Ofir on 23-Mar-17.
*/
public class smc extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_page);
int cc = 690;
int weight = 150;
int hp = 67;
int topspeed = 180;
ImageView logo = (ImageView) findViewById(R.id.logo_image);
logo.setImageResource(R.drawable.smc);
TextView Displacement = (TextView) findViewById(R.id.displacement_text);
Displacement.setText(cc + "cc");
TextView HP = (TextView) findViewById(R.id.hp_text);
HP.setText(""+hp);
TextView Weight = (TextView) findViewById(R.id.weight_text);
Weight.setText(""+weight);
TextView topSpeed = (TextView) findViewById(R.id.top_speed_text);
topSpeed.setText(topspeed + " km/h");
}
}
now my questions is, is there an easier way to achieve that? instead of setting a class for each and each model and settings tons of intents?
Upvotes: 0
Views: 59
Reputation: 60
So for all items, you got common elements (Displacement, weight, topSpeed, etc.). You can make use of the Bundle class.
It's really easy.
Make a instance of a bundle
Bundle bundle = new Bundle();
Then on the bundle, just put whatever items that you like. For example if you want to put a arraylist,
bundle.putIntArray(arrayKey, int[]);
Note: key is there so that the next activity can recognize which item you want in the bundle.
Naturally, put the bundle on your intent.
intent.putExtra(bundleKey, bundle);
In your next activity, get intent, then your bundle.
Bundle bundle = getIntent().getExtras().getBundle(bundleKey);
Extract your arraylist from the bundle.
int yourArrayList[] = bundle.getIntArray(arrayKey);
And you're all done!
Upvotes: 1
Reputation: 191728
like putting ALL of the data in one class and just pull it from it?
I'd suggest that you make one Bike.java
file that holds all information about a single Bike type (speed, image, CC, weight).
Then, you can have Bike smc = new Bike(690, 150, 67, 180, R.drawable.smc)
.
And you can load that into only one BikeDetailActivity.java
using Intents.
And make a BikeAdapter.java
that's an ArrayAdapter<Bike>
so that when you click on any item, you just make an Intent and pass on the necessary information to display there.
You'll also need to learn to use getIntent
to get the data.
Note: a database will be useful if you want to edit and save your bike information.
Upvotes: 0