sqlite的问题。小白求救。。

llakcs llakcs 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大大 问题如下。。 想把数据库的数据用listview显示出来.在插入数据时候用键值对显示在listview上
但就是 ID这一个显示为null. 下面为代码


java code:

sqldbhelp.java

public class DBhelp extends SQLiteOpenHelper{
private final static int DataBaseVesion= 1;
private final static String DATABASE_NAME="DataBase.db";
public DBhelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists user(id integer AUTOINC primary key ,name varchar(20) not null , age INTEGER not null )");
db.execSQL("insert into user(id,name,age)values(?,?,?)",new Object[]{1,"feifeishijie",18});
db.execSQL("insert into user(id,name,age)values(?,?,?)",new Object[]{2,"cccccc",20});
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}




mainactivity.java



public class MainActivity extends Activity implements OnClickListener,
OnItemSelectedListener {
private ListView listView;
private Button saveButton;
private EditText editText_Name;
private EditText editText_Age;
private SQLiteDatabase sqLiteDatabase;
private LayoutInflater inflater;
private String[] provinces = new String[]{"编辑","删除","帮助"};

DBhelp dBhelp;
Cursor cursor;
List<Map<String, Object>> items;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listview1);
saveButton = (Button) findViewById(R.id.save_button);
editText_Name = (EditText) findViewById(R.id.EditText_Name);
editText_Age = (EditText) findViewById(R.id.EditText_Age);
saveButton.setOnClickListener(this);
dBhelp = new DBhelp(this, "DataBase.db", null, 1);
sqLiteDatabase = dBhelp.getReadableDatabase();
cursor = sqLiteDatabase.rawQuery("select id ,name,age from user", null);
items = new ArrayList<Map<String, Object>>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Map<String, Object> item = new HashMap<String, Object>();
// int nameindex=cursor.getColumnIndex("name");
// String nameString=cursor.getColumnName(nameindex);
// int ageindex=cursor.getColumnIndex("age");
// String ageString=cursor.getColumnName(ageindex);
String id = cursor.getString(cursor.getColumnIndex("id"));
Log.d("id_____________________!", id+"");
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
item.put("id", id);
item.put("name", name);
item.put("age", age);
//System.out.println("namestring------>" + name);
items.add(item);
}
adapter = new ListAdapter(items);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(this);
}
public void refsesh(){
dBhelp = new DBhelp(this, "DataBase.db", null, 1);
sqLiteDatabase = dBhelp.getReadableDatabase();
cursor = sqLiteDatabase.rawQuery("select id,name,age from user", null);
items = new ArrayList<Map<String, Object>>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Map<String, Object> item = new HashMap<String, Object>();
// int nameindex=cursor.getColumnIndex("name");
// String nameString=cursor.getColumnName(nameindex);
// int ageindex=cursor.getColumnIndex("age");
// String ageString=cursor.getColumnName(ageindex);
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
item.put("id", id);

item.put("name", name);
item.put("age", age);
//System.out.println("namestring------>" + name);
items.add(item);
Log.d("id_____________________________!", String.valueOf(id));
}
adapter = new ListAdapter(items);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(this);

}


class ListAdapter extends BaseAdapter {
public List<Map<String, Object>> list;

public ListAdapter(List<Map<String, Object>> list) {
this.list = list;
}


@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int positon) {
// TODO Auto-generated method stub
return list.get(positon);
}

@Override
public long getItemId(int positon) {
// TODO Auto-generated method stub
return positon;
}

@Override
public View getView(int positon, View contenView, ViewGroup parent) {

inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.item, null);
TextView Text_id = (TextView) view.findViewById(R.id.Text_id);
TextView Text_name = (TextView) view.findViewById(R.id.Text_name);
TextView Text_age = (TextView) view.findViewById(R.id.Text_age);
Map<String, Object> map = list.get(positon);
// System.out.println("map.get(name)------->"+map.get("name"));
Text_id.setText(map.get("id")+"");
Text_name.setText(map.get("name") + "");
Text_age.setText(map.get("age") + "");
return view;
}
}


@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;
}

@Override
public void onItemSelected(AdapterView<?> parent, View contenView, int positon,
long id) {
// TODO Auto-generated method stub
Log.d("onitem", "onitemselected");
new AlertDialog.Builder(MainActivity.this).setTitle("123").
setItems(provinces, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

switch (which) {

case 0:
Toast.makeText(MainActivity.this, "case______________!000", Toast.LENGTH_SHORT).show();
break;

case 1:
break;

case 2:
break;
}
Log.d("which", String.valueOf(which));
}
}).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

//保存按钮
public void save() {
DBhelp dbHelper = new DBhelp(this, "DataBase.db", null, 1);
sqLiteDatabase = dbHelper.getWritableDatabase();
sqLiteDatabase.beginTransaction();
String editname = editText_Name.getText().toString().trim();
Integer age = Integer.parseInt(editText_Age.getText().toString().trim());
if (editname != null) {
if (age !=null) {

sqLiteDatabase.execSQL("insert into user(name,age)values(?,?)",
new Object[] { editname, age });
Log.d("editname_________________________!", editname + "" + age);
sqLiteDatabase.setTransactionSuccessful();
sqLiteDatabase.endTransaction();
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
//刷新map

/*Map<String,Object> map=new HashMap<String, Object>();
map.put("name",editname);
map.put("age",age);
adapter.list.add(map);
adapter.notifyDataSetChanged();*/

} else {
Toast.makeText(this, "输入的类型不正确,请重新输入", Toast.LENGTH_SHORT)
.show();
editText_Age.setText("");
}
} else {
Toast.makeText(this, "输入的类型不正确,请重新输入", Toast.LENGTH_SHORT).show();
editText_Name.setText("");
}

}


@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.save_button:
save();
refsesh();
break;

default:
break;
}
}

}





...全文

相关推荐

SQLite 轻型数据库

SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义、表、索引和数据本身)都保存在一个单一的文件中。并且,SQLite是一个用C实现的类库,它在内存消耗、文件体积、简单性方面都有不错的表现,如果数据在10W条以下,查询速度也是相当快的。 SQLite具有以下特征: 实现多数SQL92的标准,包括事务(原子性、一致性、隔离性和持久性)、触发器和大多数的复杂查询。 不对插入或者更新的数据进行类型检查,你可以将字符串插入到整数列中(这个可能让有些用户不太适应)。 支持Windows/Linux/Unix等主流系统,还支持嵌入式系统如Android或Windows Mobile。

xyjboy

sqlite3.exe

sqlite3.exe是款强大的轻型数据库管理系统,它的设计目标是嵌入式的,目前在很多嵌入式产品中使用了它,占用资源低,支持Windows/Linux/Unix等等主流的操作系统,同时能跟很多程序语言相结合,如java,python,nodejs,同样比起Mysql、PostgreSQL这两款开源世界着名的数据库管理系统来讲,它的处理速度比他们都快。 sqlite3命令提速调试方法 在调试Android上层系统时,经常需要改动到跟数据库相关的参数,光有SQL语句使用知识毫无用处,好在Android命令行下有sqlite3命令,而现在系统的应用会把数据保存到/data/data/包名/database/目录下,该目录下会有.db扩展名的数据库文件,关于该命令的使用如下例: 1、打开指定数据库: sqlite3 /data/data/com.android.providers.settings/databases/settings.db 2、打开数据库后进入sqlite>命令行,可以使用.help查看可用指令了,也可以使用标准的SQL语句进行调试使用如update

开心的哒哒哒

sqlite 3 (轻型数据库) For Windows绿色免费版

短小精悍,易用,移动开发必备, 功能齐全,什么也不说了下下来用了你就知道他的强悍!

尋找大饅頭

sqlite3一站下载

SQLite(轻型数据库)V3.7.5 For Windows绿色免费版 sqlite java驱动 jar包 包括一个例子 navicat for sqlite客户端 已注册版本 sqlite manager sqlite客户端(轻量级) sqlite相关一站式下载

lingdolinux

安卓直播视频播放流媒体IPCameraRTSPDLNA相关-播放本地视频有Logo界面注册登录界面SeekBar进度条SQLite轻型数据库播放列表暂未实现支持跳出播放界面返回后不黑屏。自己做的毕业设计请多多指教.rar

播放本地视频,有Logo界面、注册登录界面、SeekBar进度条、SQLite轻型数据库、播放列表(暂未实现)、支持跳出播放界面返回后不黑屏。自己做的毕业设计,请多多指教.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

weixin_39841365
评论 15
收藏 已收藏
分享
返回顶部
llakcs
Android 80,489 成员 91,375 内容