问题描述
- 【初学安卓】微型数据库卡死
-
在模拟器里还能运行,但是多次滚动后就会卡住。放在手机上就闪退。望高手给出指引。这是MainActivity里的代码
package com.itheima.db;import java.util.List;
import org.w3c.dom.Text;
import com.itheima.db.dao.PersonDao;
import com.itheima.db.domain.Person;import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;public class MainActivity extends Activity {
private ListView lv;
private List persons;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PersonDao dao = new PersonDao(this); persons = dao.findAll(); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new MyAdapter()); } //默认实现类 simpleXX defaultXX baseXX private class MyAdapter extends BaseAdapter{ /* * 控制listview里面总共的条目 * */ @Override public int getCount() { return persons.size();//条目个数 == 集合的size } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(getApplicationContext()); tv.setTextSize(20); tv.setTextColor(Color.BLACK); //得到某个位置对应的person对象 Person person = persons.get(position); tv.setText(person.toString()); return tv; } }
}
这是activity里面的代码
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_root"
tools:context=".MainActivity" ><ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView>