-1

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.

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
Mohamed Mosaed
  • 105
  • 1
  • 8
  • 1
    It's best to store large data in an `SQLite` database, show a list to user using a `CursorAdapter`, and filter by querying with `LIKE` selection. – Yaroslav Mytkalyk Dec 23 '16 at 18:40

2 Answers2

1

There is not always a "best", it really depends on your needs. If this list will never change, I don't see a problem with pre-loading them into a List like you have done, except I would make the list unmodifiable:

public class Globals {
    // other classes can refer to this directly with Globals.FOOD_CAL_LIST
    public static final List<FoodCal> FOOD_CAL_LIST = 
            Collections.unmodifiableList(buildFoodCalList());

    private static List<FoodCal> buildFoodCalList() {
        List<FoodCal> list = new ArrayList<>();
        list.add(new FoodCal("Crab fresh","200 cals","110 cals"));
        list.add(new FoodCal("Duck roast","400 cals","430 cals"));
        ...
        return (FoodCalList);
    }
}

If your list needs to be able to change, then it depends how your list can change. If you are always the person providing the list, then it sounds like what you need is a place to host the latest information and your app needs to download that from time to time to update what is on the device. There numerous libraries for making network calls, it's up to you which one you want to use. Additionally, how you present that data and how you parse it is largely up to you. You can do it with JSON, XML, a CSV file, etc.

One thing that does need changing is the way you filter your list. Your filter method calls notifyDataSetChanged(), which needs to be done from the main thread. This means you are running the filter operation on the main thread, which is bad because this takes time and blocks the UI thread during this time.

The proper way to do that is for your adapter to implement the Filterable interface and place this logic in a subclass of Filter. You can see a complete example in this answer.

Community
  • 1
  • 1
Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

If your list needs change then you need to change the code, its better to create a json file and parse the json objects to create class type objects. Follow the link to know about json parsing https://developer.android.com/reference/android/util/JsonReader.html