一点listview里面的项应用就崩溃MainActivity.java文件的代码:
public class MainActivity extends Activity { int flag;
ListView lv;
Dialog alertDialog;
int _id;

@Override
protected void onCreate(Bundle savedInstanceState) {
flag = 0;
_id=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lv_userlist);
ScreenDisplay(++flag,_id);

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap item = (HashMap) arg0.getItemAtPosition(arg2);
Integer _id = Integer.parseInt(String.valueOf(item.get("_id")));
ScreenDisplay(++flag,_id);
}
});

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
HashMap item = (HashMap) arg0.getItemAtPosition(arg2);
Integer _id = Integer.parseInt(String.valueOf(item.get("_id")));
RelativeLayout r = (RelativeLayout) arg1;
return true;
}
});
// 为list添加item选择器
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void ScreenDisplay(int flag,int _id) {
ArrayList list;
SimpleAdapter adapter = null;
DBProvider provider;
// 得到用于存放listView的linearLayout
LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.id.list_ll);
// 打开数据库
provider = new DBProvider(this);
provider.openDatabase();

switch (flag) {
case 1:
list = provider.getAllRecords();
// if (list.size() == 0) {
records re = new records();
re._id = 1;
re.uid = 1;
re.phone = "10086";
  long result = provider.insertRecords(re);

user user = new user();
user._id = 1;
user.name = "a";
user.phone1 = "10086";
user.phone2 = "10000";

// Drawable nodata_bg = getResources().getDrawable(
// R.drawable.nodata_bg);
// mainLinearLayout.setBackgroundDrawable(nodata_bg);
setTitle("没有站点数据");
// }
// 拥有所有数据的Adapter

list = provider.getAllRecords();
// adapter.notifyDataSetChanged();
adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.records, new String[] { "phone" },
new int[] { R.id.txt_name });
break;
default:
flag = 0;
break;
}
lv.setAdapter(adapter);
// 将整合好的adapter交给listview,显示给用户看*/
}
}DBProvider.java文件的代码:
public class DBProvider {
public static final String DB_DBNAME = "contact";

public static final int VERSION = 4;

private Context context;

public DBProvider(Context context) {
this.context = context;
}

public void openDatabase() {
if (dbInstance == null) {
myDBHelper = new MyDBHelper(context, DB_DBNAME, VERSION);
dbInstance = myDBHelper.getWritableDatabase();
}
}

public static SQLiteDatabase dbInstance;
private MyDBHelper myDBHelper;
private StringBuffer tableCreate;

/*建表*/
class MyDBHelper extends SQLiteOpenHelper { public MyDBHelper(Context context, String name, int version) {
super(context, name, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
/*用户表*/
tableCreate = new StringBuffer();
tableCreate.append("create table ").append("user")
.append(" (")
.append("_id integer primary key ,")
.append("name text,")
.append("phone1,")
.append("phone2")
.append(")");
db.execSQL(tableCreate.toString());
/*记录表*/
tableCreate = new StringBuffer();
tableCreate.append("create table ").append("records")
.append(" (")
.append("_id integer primary key ,")
.append("uid intrger,")
.append("phone text")
.append(")");
System.out.println(tableCreate.toString());
db.execSQL(tableCreate.toString());
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists user";
db.execSQL(sql);
myDBHelper.onCreate(db);
} }
/*插入*/
/*记录表*/
public long insertRecords(records re) {
ContentValues values = new ContentValues();
values.put("_id", re._id);
values.put("uid", re.uid);
values.put("phone", re.phone);
return dbInstance.insert("records", null, values);
}
/*用户表*/
public long insertUser(user user){
ContentValues values = new ContentValues();
values.put("_id", user._id);
values.put("name", user.name);
values.put("phone1", user.phone1);
values.put("phone2", user.phone2);
return dbInstance.insert("user", null, values);
}

public ArrayList getAllRecords() {
ArrayList list = new ArrayList();
Cursor cursor = null;
cursor = dbInstance.query("records", new String[] { "_id", "uid",
"phone" }, null, null, null,
null, null);

while (cursor.moveToNext()) {
HashMap item = new HashMap();
item.put("uid", cursor.getInt(cursor.getColumnIndex("uid")));
item.put("phone", cursor.getString(cursor.getColumnIndex("phone")));
list.add(item);
}
return list;
}

public ArrayList getAllUser(int _id){
ArrayList list = new ArrayList();
Cursor cursor = null;
cursor = dbInstance.query("user", new String[] {"_id", "name", "phone1", "phone2"}, 
"_id = " + _id, null, null, null, null);

while(cursor.moveToNext()){
HashMap item = new HashMap();
item.put("name", cursor.getString(cursor.getColumnIndex("name")));
item.put("phone1", cursor.getString(cursor.getColumnIndex("phone1")));
item.put("phone2", cursor.getString(cursor.getColumnIndex("phone2")));
list.add(item);
}
return list;
}

public void deleteUser(int uid) {
dbInstance.delete("user", "_id=?",
new String[] { String.valueOf(uid) });
}
public void deleteRecords(int rid){
dbInstance.delete("records", "_id=?", 
new String[] {String.valueOf(rid) });
}

public ArrayList getRecords() {
ArrayList list = new ArrayList();
String strSelection = "";
String sql = "select * from records"; Cursor cursor = dbInstance.rawQuery(sql, null);
while (cursor.moveToNext()) {
HashMap item = new HashMap();
item.put("_id", cursor.getInt(cursor.getColumnIndex("_id")));
item.put("name", cursor.getString(cursor.getColumnIndex("name")));
item.put("phone", cursor.getInt(cursor.getColumnIndex("phone")));
list.add(item);
}
return list;
}
}