虽然列表中存在列_ID但不存在错误

时间:2014-02-15 12:43:45

标签: android android-listview android-sqlite

虽然我在表格列和查询中添加了java.lang.IllegalArgumentException: column '_id' does not exist,但我反复收到_ID并且应用程序崩溃了。

这里我试图从数据库中取出数据并使用自定义游标适配器显示列表视图。

表类

public class StockTable {

    Context c1;
    DatabaseObject  d1;
    Cursor c;

    StockTable(Context mcontext){
        c1=mcontext;
        //---------------objects---------------------------//
        d1=new DatabaseObject(c1);
        //---------------objects ends----------------------//
    }
    // d1=new DatabaseObject(c1);
    //-----------General Decelerations-------------------//
    private String selectID;
    private int storeID=0;
    //-----------General Decelerations ends--------------//
    //-----------table name and columns-----------------//
    final String tablename="StockTable";
    //public static final String _ID = "ID";
    public String column1=" _ID";
    public String column2="StockName";

    //-----------table name and columns end-----------------//

    final String stocktable = "CREATE TABLE " + tablename + 
                " (" + column1 + " INTEGER PRIMARY KEY , " + column2 + " TEXT ) ";

    public ContentValues insert(String one){

        try{
        selectID="Select Max("+  column1 + ") from " + tablename;
        System.out.println(selectID);
        c=d1.d.db.rawQuery(selectID,null);
        System.out.println(c.getCount());
        //System.out.println(c.getInt(0));
        if(c.moveToNext())
        {

            System.out.println("Has Values");
        }
        else
        {

            System.out.println("No Values");
        }
        }
        catch(Exception e)
        {

            System.out.println(e.getMessage());
        }

        ContentValues cvi=new ContentValues();
    //  for(int i=0;i<=1;i++)
            cvi.put(column1, c.getInt(0)+1);
            cvi.put(column2,one);


        return cvi;
    }

    public void delete(){


    }

    public void select(){


    }

}

填充列表视图的类。

public class stockmanager extends Activity{

    /*public stockmanager() {
        // TODO Auto-generated constructor stub
        populatelist pl=new populatelist();
        //pl.getView(position, getCurrentFocus(), null);
    }*/
    String getentry;
    private int storeID=0;
    DatabaseObject d;
    StockTable st;
    private String getstocks;
    public Cursor a1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);
        d=new DatabaseObject(getApplicationContext());
        st=new StockTable(getApplicationContext());
    final Button AddStock=(Button) findViewById(R.id.button1);
         final EditText entry=(EditText) findViewById(R.id.editText1);
        final Button BroDetail=(Button) findViewById(R.id.button2);
        final ListView popstocks=(ListView) findViewById(R.id.listView1);

        AddStock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                getentry=entry.getText().toString();
                //st.insert(getentry);
                System.out.println(getentry);
                //d.db.rawQuery(st.select(), null);
                d.d.db.insert(st.tablename, null,st.insert(getentry));
                //populatelist populatestocks=new populatelist();

                getstocks="Select " + st.column1 + " as _ID, " + st.column2 + " From "+ st.tablename;
                 System.out.println(getstocks);
                a1=d.d.db.rawQuery(getstocks, null);
                if(a1.moveToNext()){

                    System.out.println(a1.getCount());  
                }
                else{
                    System.out.println("can't open database");
                }

                poplist populatestocks=new poplist(getApplicationContext(),a1) ;
                popstocks.setAdapter(populatestocks);

            }
        });
                }


    public class poplist extends CursorAdapter{

        public poplist(Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
        }
        //StockTable st1=new StockTable(getApplicationContext());
        //Database d1=new Database(getApplicationContext());


        @Override
        public void bindView(View view, Context context, Cursor c) {
            // TODO Auto-generated method stub
            final CheckBox cb=(CheckBox) view.findViewById(R.id.checkBox1);
            final Button view1=(Button) view.findViewById(R.id.button1); 

            if(c.moveToFirst()){

                //cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
                    //do{
                        //cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
                cb.setText(c.getString(1));
                //  }while (a1.moveToNext());

                }

        }

        @Override
        public View newView(Context context, Cursor c, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(R.layout.stocklist, parent, false);
                    bindView(v, context, c);
                   return v;
//          return null;
        }


    }

    public void Declerations(){


    }


}

请帮助我解决这个问题,因为我在过去2天内一直在与此作斗争。

1 个答案:

答案 0 :(得分:4)

就Android SQLite而言,列名称区分大小写。您需要_id而不是_ID。 (在纯SQL中,标识符不区分大小写。)