我在堆中有超过64kb的数据,我怎样才能克服限制?
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
}
});
}
}
以下是问题https://dl.dropbox.com/u/15065300/problem-4.png
的图片我是android的新手,所以需要精心的建议。
答案 0 :(得分:1)
Java将方法的大小限制为65535字节。您可以做的是将onCreate方法分解为更小的方法,然后从onCreate中调用它们。
编辑:
您可以做的是创建一个单独的方法来填充您的列表。在创建列表适配器之前,您需要调用它。
注意:尽管正如其他人所提到的那样,如果你有这么多硬编码的值,通常会被认为是不好的做法。建议将其更新为从DataBase查询,或从某种类型的文件(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.");
}
}
答案 1 :(得分:1)
你没有。您需要将数据存储在SQLite database中,或从文件中读取数据。一个方法中的代码不能超过64kB,虽然将它分成多个方法会起作用,但这是一种糟糕的编码实践。
我非常强烈建议您将数据保存在SQLite数据库中。 Here是一个很好的教程。