我有一个产品列表,但Java抱怨该方法超出65535字节的限制。如何添加更多单词并克服限制?
public class ProductList extends Activity {
// List view
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");
String products[] = {
// I want these words to keep in a database
// Because here is 65kb limit but I need more...
"Banana" +as+ "Its color is yellow." ,
"Orange" +as+ "Orange is a sour fruit." ,
"Onion" +as+ "Onion usually used on Pizza" ,
"Google" +as+ "Google is a giant search engine." ,
"Love" +as+ "Love is related to heart." ,
"Lily" +as+ "It is one kind of white flower." ,
"Banana" +as+ "Its color is yellow." ,
"Orange" +as+ "Orange is a sour fruit." ,
"Onion" +as+ "Onion usually used on Pizza" ,
"Google" +as+ "Google is a giant search engine." ,
"Love" +as+ "Love is related to heart." ,
"Lily" +as+ "It is one kind of white flower." ,
"Banana" +as+ "Its color is yellow." ,
"Orange" +as+ "Orange is a sour fruit." ,
"Onion" +as+ "Onion usually used on Pizza" ,
"Google" +as+ "Google is a giant search engine." ,
};
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
}
});
}
}
为了更容易理解:
提前致谢
答案 0 :(得分:8)
据我所知,这不是对Sqlite的限制 - Eclipse错误表明它是方法的问题。
我强烈建议如果您有大量数据,则应将其保存在单独的资源中。然后在执行时加载资源。没有必要将它烘焙到您的代码中。如果需要,您可以随时添加一些字符串以替换平台行分隔符。
答案 1 :(得分:2)
您正在堆栈上分配该数组,并且堆栈通常具有64kb的限制。它可以容纳多少。
您的解决方案是使用堆。如果您将代码更改为
// allocate an array on the heap
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");
// etc
然后我打赌这会奏效。
然而,您最好的选择是执行Jon Skeet所说的内容并将所有数据存储在资源中并在需要时加载它。由于看起来你正在使用Android,我建议使用String Arrays,这就是你应该如何处理这类数据。
<强>更新强>
有趣。事实证明这是一个Java限制。见this answer。我的理解是类文件中的跳转偏移是16位,这意味着方法最多可以是65535字节。
因此黑客解决方案是将其拆分为更小的方法:
// allocate an array on the heap
ArrayList<String> products = new ArrayList<String>();
addFirstThousand(products);
addSecondThousand(products);
// etc.
private void addFirstThousand(ArrayList<String> products) {
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");
// etc
}
但这是一个 可怕的 主意。最好将这些字符串放在某种数据文件中,然后在运行时加载它。如果由于某种原因你真的反对做正确的事并使用谷歌为你提供的String Array,那么你也可以将这些字符串放在资产中的文本文件中,并通过正常情况读取它Java BufferedReader
。
答案 2 :(得分:0)
解决方案是将对象动态分配到堆中,这实际上是无限的。