0

Possible Duplicate:
How to exceed 65535 byte limit for method code

I have more then 64kb data in heap, How can I overcome the limit?

public class ProductList extends Activity {

    //Listview
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;

    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);


        String as = System.getProperty("line.separator");


                ArrayList<String> products = new ArrayList<String>();

                products.add("Banana" +as+ "Its color is yellow.");
                products.add("Orange" +as+ "Orange is a sour fruit.");
                products.add("Onion" +as+ "Onion usually used on Pizza");
                products.add("Banana" +as+ "Its color is yellow.");
                products.add("Orange" +as+ "Orange is a sour fruit.");
                products.add("Onion" +as+ "Onion usually used on Pizza");
                products.add("Banana" +as+ "Its color is yellow.");
                products.add("Orange" +as+ "Orange is a sour fruit.");
                products.add("Onion" +as+ "Onion usually used on Pizza");
                products.add("Banana" +as+ "Its color is yellow.");
                products.add("Orange" +as+ "Orange is a sour fruit.");




        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.p_list,   products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                ProductList.this.adapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });


    }

}

Here is the image of the problem https://dl.dropbox.com/u/15065300/problem-4.png

I am new to android so need elaborate suggestion.

Community
  • 1
  • 1
Jani
  • 133
  • 2
  • 7
  • Its very bad programming practice to hardcode all your data into your code. You need to use a file or database separate from your code – CocoNess Dec 13 '12 at 18:03
  • http://stackoverflow.com/questions/6570343/maximum-size-of-a-method-in-java – Ali Imran Dec 13 '12 at 18:05
  • 1
    No I cant help you restructure your code. There are many tutorials about using sqllite database. I suggest you search for one – CocoNess Dec 13 '12 at 18:08

2 Answers2

1

Java limits the size of methods to 65535 bytes. What you can do is break your onCreate method up into smaller methods and then call those from within your onCreate.

Edit:

What you can do is create a separate method that will populate your list. You'll want to call this before you create the list adapter.

Note: Though, as others have mentioned, it is typically considered bad practice if you have this many values hard-coded like this. Suggest updating it to either query from a DataBase, or read from some type of file (txt, xml).

public class ProductList extends Activity 
{
    //Listview
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;

    ArrayList<String> products = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        initProductList();
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.p_list, products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() 
        {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                ProductList.this.adapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });
    }

    private void initProductList()
    {
        String as = System.getProperty("line.separator"); 
        products.add("Banana" +as+ "Its color is yellow.");
        products.add("Orange" +as+ "Orange is a sour fruit.");
        products.add("Onion" +as+ "Onion usually used on Pizza");
        products.add("Banana" +as+ "Its color is yellow.");
        products.add("Orange" +as+ "Orange is a sour fruit.");
        products.add("Onion" +as+ "Onion usually used on Pizza");
        products.add("Banana" +as+ "Its color is yellow.");
        products.add("Orange" +as+ "Orange is a sour fruit.");
        products.add("Onion" +as+ "Onion usually used on Pizza");
        products.add("Banana" +as+ "Its color is yellow.");
        products.add("Orange" +as+ "Orange is a sour fruit.");
    }
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • Could you please give me elaborate suggestion. Here is my code http://www.mediafire.com/?83bol5mhgromgg0 – Jani Dec 13 '12 at 18:14
  • I might not implemented correctly! It shows the same error https://dl.dropbox.com/u/15065300/problem-5.png – Jani Dec 13 '12 at 18:36
  • Could you please return me the re-structured app – Jani Dec 13 '12 at 18:37
1

You don't. You need to store your data in either an SQLite database, or read it from a file. You cannot have more than 64kB worth of code in a single method, and while splitting it into multiple methods will work, it is a terrible coding practice.

I would very strongly advise you to keep your data in an SQLite database. Here is an excellent tutorial for doing that.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195