sqlite的问题。小白求救。。

llakcs 2013-09-12 03:38:18
各位大大 问题如下。。 想把数据库的数据用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;
}
}

}





...全文
395 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
llakcs 2013-09-13
  • 打赏
  • 举报
回复
引用 3 楼 tanwei4199 的回复:
create table if not exists user(id integer AUTOINC primary key ,name varchar(20) not null , age INTEGER not null )"); 第一个id我看到的都是 _id 不知道是不是问题所在 我也很少用 你可以试试 另外 那个点击事件不是onitemselectlistener 而是onitemClicklistener onitemselectlistener 是焦点事件(即当前的光标——如果有的话——落在了某个item上) 你打下log看看就知道了
打印显示onitemselectlistener没被调用. 一定要_id嘛?
荒颜 2013-09-13
  • 打赏
  • 举报
回复
create table if not exists user(id integer AUTOINC primary key ,name varchar(20) not null , age INTEGER not null )"); 第一个id我看到的都是 _id 不知道是不是问题所在 我也很少用 你可以试试 另外 那个点击事件不是onitemselectlistener 而是onitemClicklistener onitemselectlistener 是焦点事件(即当前的光标——如果有的话——落在了某个item上) 你打下log看看就知道了
llakcs 2013-09-13
  • 打赏
  • 举报
回复
后来插入都是0....
llakcs 2013-09-13
  • 打赏
  • 举报
回复
引用 13 楼 rui888 的回复:
[quote=引用 12 楼 llakcs 的回复:] 我的打印显示为0。 [quote=引用 11 楼 rui888 的回复:] [quote=引用 10 楼 llakcs 的回复:] [quote=引用 9 楼 rui888 的回复:] String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
已经改了。这次显示在列表上面的值为0。而不是null.[/quote] 你看下你的数据库是多少啊。[/quote][/quote] 你看下insert 之后sqllite 里面是什么啊。 你多插入几条是不是都是0 [/quote] 初始创建数据库调用
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});
是有插入_id的。 但是后来向数据库插入数据时候如下:
sqLiteDatabase.execSQL("insert into user(name,age)values(?,?)",
                        new Object[] { editname, age });
顺便问下。为啥。onitemclicklistenner和onitemselectedlistener 里面都加了打印但在虚拟机里面用鼠标单击的时候,打印显示没有触发?
tony4geek 2013-09-13
  • 打赏
  • 举报
回复
引用 12 楼 llakcs 的回复:
我的打印显示为0。 [quote=引用 11 楼 rui888 的回复:] [quote=引用 10 楼 llakcs 的回复:] [quote=引用 9 楼 rui888 的回复:] String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
已经改了。这次显示在列表上面的值为0。而不是null.[/quote] 你看下你的数据库是多少啊。[/quote][/quote] 你看下insert 之后sqllite 里面是什么啊。 你多插入几条是不是都是0
llakcs 2013-09-13
  • 打赏
  • 举报
回复
我的打印显示为0。
引用 11 楼 rui888 的回复:
[quote=引用 10 楼 llakcs 的回复:] [quote=引用 9 楼 rui888 的回复:] String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
已经改了。这次显示在列表上面的值为0。而不是null.[/quote] 你看下你的数据库是多少啊。[/quote]
tony4geek 2013-09-13
  • 打赏
  • 举报
回复
引用 10 楼 llakcs 的回复:
[quote=引用 9 楼 rui888 的回复:] String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
已经改了。这次显示在列表上面的值为0。而不是null.[/quote] 你看下你的数据库是多少啊。
llakcs 2013-09-13
  • 打赏
  • 举报
回复
引用 9 楼 rui888 的回复:
String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
已经改了。这次显示在列表上面的值为0。而不是null.
tony4geek 2013-09-13
  • 打赏
  • 举报
回复
String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
llakcs 2013-09-13
  • 打赏
  • 举报
回复
public class MainActivity extends Activity implements OnClickListener,OnItemClickListener{
	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);
			int id = cursor.getInt(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);
		
		
	}


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

	
	


	//保存按钮
	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;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int positon, long id) {
		// TODO Auto-generated method stub
		Log.d("onitemclick____________________!!", "onitemclick____________________onitemclick____________________");
		
		
	}
	
引用 5 楼 CuGBabyBeaR 的回复:
[quote=引用 4 楼 llakcs 的回复:] [quote=引用 3 楼 tanwei4199 的回复:] create table if not exists user(id integer AUTOINC primary key ,name varchar(20) not null , age INTEGER not null )"); 第一个id我看到的都是 _id 不知道是不是问题所在 我也很少用 你可以试试 另外 那个点击事件不是onitemselectlistener 而是onitemClicklistener onitemselectlistener 是焦点事件(即当前的光标——如果有的话——落在了某个item上) 你打下log看看就知道了
打印显示onitemselectlistener没被调用. 一定要_id嘛?[/quote] Android系统自己的所有数据库主键都是 _id 你的代码能放进代码标签里么 我根本不想看 [/quote]
荒颜 2013-09-13
  • 打赏
  • 举报
回复
引用 4 楼 llakcs 的回复:
[quote=引用 3 楼 tanwei4199 的回复:] create table if not exists user(id integer AUTOINC primary key ,name varchar(20) not null , age INTEGER not null )"); 第一个id我看到的都是 _id 不知道是不是问题所在 我也很少用 你可以试试 另外 那个点击事件不是onitemselectlistener 而是onitemClicklistener onitemselectlistener 是焦点事件(即当前的光标——如果有的话——落在了某个item上) 你打下log看看就知道了
打印显示onitemselectlistener没被调用. 一定要_id嘛?[/quote] 那就是没用了 我之前使用模拟器的上下键 是可以得到相应的 onitemClicklistener 这个才是点击事件的 用这个吧
胖虎 2013-09-12
  • 打赏
  • 举报
回复
你这样扔出来 很难看耶~~~也没注释啥的~嘿嘿,我是没耐性看哈,等高手帮你解决吧~~
llakcs 2013-09-12
  • 打赏
  • 举报
回复
顺便追问下、为啥onitemselectlistener没有触发。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧