I'm new to Android development and try to develop my first test app. I have a best practice question regarding display more than 500 String and apply search.
I have the following sample for food calories list:
Type - Portion size - per 100 grams(3.5 oz)
Crab fresh - 200 cals - 110 cals
Duck roast - 400 cals - 430 cals
Beef (roast) - 300 cals - 280 cals
Beef burgers frozen - 320 cals - 280 cals
Chicken - 220 cals - 200 cals
etc..... for more than 500 types of food.
Is it safe to do the following or What it is the best practice?
public class FoodCal {
public String Type;
public String PortionSize;
public String PerGram;
public FoodCal(String type, String portionSize, String perGram) {
this.Type = type;
this.PortionSize = portionSize;
this.PerGram = perGram;
}
}
Global class:
public class Global {
public static ArrayList<FoodCal> GetFoodCalList() {
ArrayList<FoodCal> FoodCalList = new ArrayList<>();
FoodCalList.add(new FoodCal("Crab fresh","200 cals","110 cals"));
FoodCalList.add(new FoodCal("Duck roast","400 cals","430 cals"));
etc.......
return (FoodCalList);
}
}
FoodListAdapter Class
class FoodListAdapter extends BaseAdapter {
ArrayList<SystemName> foodListLocal;
ArrayList<SystemName> fullFoodList;
FoodListAdapter() {
foodListLocal = Global.GetFoodCalList();
}
public void filter(String charText) {
fullFoodList = Global.GetFoodCalList();
foodListLocal.clear();
if (charText.length() == 0) {
foodListLocal = Global.GetFoodCalList();
} else {
for (FoodCal food : fullFoodList) {
if (food.Type.contains(charText)) {
foodListLocal.add(food);
}
}
}
//Notify UI
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return foodListLocal.size();
}
@Override
public Object getItem(int position) {
return foodListLocal.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = getLayoutInflater();
View myView = mInflater.inflate(R.layout.food_view, null);
TextView txtFoodName = (TextView) myView.findViewById(R.id.txtFoodName);
TextView txtSize = (TextView) myView.findViewById(R.id.txtSize);
TextView txtPerGrams = (TextView) myView.findViewById(R.id.txtPerGrams);
FoodCal temp = foodListLocal.get(position);
txtFoodName.setText(temp.Type);
txtSize.setText(temp.PortionSize);
txtPerGrams.setText(temp.PerGram);
return myView;
}
}
Should I use Json file, Is it will have good performance ? If Json better, Please write the code to do it or the best practice for that.
also please specify if my code need more enhancement some where.
Thanks in advance.