本帖最后由 llakcs 于 2013-09-12 15:42:24 编辑

解决方案 »

  1.   

    顺便追问下、为啥onitemselectlistener没有触发。
      

  2.   

    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看看就知道了
      

  3.   


    打印显示onitemselectlistener没被调用.    一定要_id嘛?
      

  4.   


    打印显示onitemselectlistener没被调用.    一定要_id嘛?
    那就是没用了  我之前使用模拟器的上下键  是可以得到相应的
    onitemClicklistener  这个才是点击事件的  用这个吧
      

  5.   

    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____________________");


    }

    打印显示onitemselectlistener没被调用.    一定要_id嘛?
    Android系统自己的所有数据库主键都是 _id 你的代码能放进代码标签里么 我根本不想看
      

  6.   

       String id = cursor.getString(cursor.getColumnIndex("id")); 你数据库的id设置是整型自动自增的
      

  7.   

    已经改了。这次显示在列表上面的值为0。而不是null.
      

  8.   

    已经改了。这次显示在列表上面的值为0。而不是null.
    你看下你的数据库是多少啊。
      

  9.   

    我的打印显示为0。已经改了。这次显示在列表上面的值为0。而不是null.
    你看下你的数据库是多少啊。
      

  10.   

    已经改了。这次显示在列表上面的值为0。而不是null.
    你看下你的数据库是多少啊。你看下insert 之后sqllite 里面是什么啊。 你多插入几条是不是都是0 
      

  11.   

    已经改了。这次显示在列表上面的值为0。而不是null.
    你看下你的数据库是多少啊。你看下insert 之后sqllite 里面是什么啊。 你多插入几条是不是都是0 初始创建数据库调用
    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  里面都加了打印但在虚拟机里面用鼠标单击的时候,打印显示没有触发?