如何从sqlite数据库中获取具有多列的数据并在simpleadapter中显示它

时间:2014-08-26 06:37:39

标签: android sqlite

我正在制作一个应用程序,当我试图在两个日期之间找到横切时,我会得到一个产品的横断面,我得到的结果只是两个日期之间的最后一个值

这是数据库代码

    public DatabaseHandler(Context context)
    {
        super(context, DB_NAME, null, DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        String CreateTableLiveRate = "CREATE TABLE liverate("
                +"product_name TEXT NOT NULL,"
                +"price TEXT)";

        String CreateTablePeople = "CREATE TABLE people("
                +"name TEXT NOT NULL,"
                +"location TEXT)";

        String CreateTableTransection = "CREATE TABLE transection("
                +"date TEXT NOT NULL,"
                +"name TEXT NOT NULL,"
                +"credit TEXT NOT NULL,"
                +"debit TEXT NOT NULL)";

        String CreateTableTotalPayment = "CREATE TABLE total_payment("
                +"name TEXT NOT NULL,"
                +"total TEXT)";

        String CreateTableEverydayTransection = "CREATE TABLE everyday_transection("
                +"date TEXT NOT NULL,"
                +"name TEXT NOT NULL,"
                +"product_name TEXT NOT NULL,"
                +"price TEXT NOT NULL,"
                +"qty TEXT NOT NULL)";

        db.execSQL(CreateTableLiveRate);
        db.execSQL(CreateTablePeople);
        db.execSQL(CreateTableTransection);
        db.execSQL(CreateTableTotalPayment);
        db.execSQL(CreateTableEverydayTransection);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {

    }

    //adding new product to liverate 
    public void addnewproduct(String Productname,String Price)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues insValues = new ContentValues();
        insValues.put("product_name", Productname);
        insValues.put("price", Price);
        db.insert("liverate", null, insValues);
    }

    //adding new vendors to people and totat_payment
    public void addnewpeople(String Name,String Location)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues insValues = new ContentValues();
        insValues.put("name", Name);
        insValues.put("location", Location);
        db.insert("people", null, insValues);

        ContentValues insValues1 = new ContentValues();
        insValues1.put("name", Name);
        db.insert("total_payment", null, insValues1);
    }

    // get total amount of the vendor
    public Cursor gettotal(String name)
    {
        Cursor result;
        SQLiteDatabase db = getReadableDatabase();
        String tot = "SELECT total from total_payment "
                +"WHERE name = '"+name+"'";
        result = db.rawQuery(tot, null);
        return result;
    }

    // get the current rate of the product from the liverate
    public Cursor getrate(String productname)
    {
        Cursor result;
        SQLiteDatabase db = getReadableDatabase();
        String rate = "SELECT price from liverate "
                +"WHERE product_name = '"+productname+"'";
        result = db.rawQuery(rate, null);
        return result;
    }

    // update total amount left for paying
    public void updatetotal(String total, String name)
    {
        SQLiteDatabase db = getWritableDatabase();
        String tot = "UPDATE total_payment "
                +"SET total = '"+total+"' "
                +"WHERE name = '"+name+"'";
        db.execSQL(tot);
    }

    // add data to transection
    public void addtransection(String Date, String Name, String Credit, String Debit)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues insValues = new ContentValues();
        insValues.put("date", Date);
        insValues.put("name", Name);
        insValues.put("credit", Credit);
        insValues.put("debit", Debit);
        db.insert("transection", null, insValues);
    }

    // add data to everyday_transection
    public void addeverydaytransection(String Date, String Name, String Product_name, String Price, String Qty)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues insValues = new ContentValues();
        insValues.put("date",Date);
        insValues.put("name",Name);
        insValues.put("product_name",Product_name);
        insValues.put("price",Price);
        insValues.put("qty",Qty);
        db.insert("everyday_transection", null, insValues);
    }

    //get the cash transection
    public Cursor getcashtransection(String Name, String FromDate, String ToDate)
    {
        Cursor result;
        SQLiteDatabase db = getReadableDatabase();
        String cash = "SELECT * from transection "
                +"WHERE date BETWEEN '"+FromDate+"' AND '"+ToDate+"' "
                +"AND name = '"+Name+"'";
        return result = db.rawQuery(cash, null);
    }

    //get product transection
    public Cursor getproducttransection(String Name, String FromDate, String ToDate)
    {
        Cursor result;
        SQLiteDatabase db = getReadableDatabase();
        String product = "SELECT * from everyday_transection"
                +"WHERE date BETWEEN '"+FromDate+"' AND '"+ToDate+"'"
                +"AND name = '"+Name+"'";
        return result = db.rawQuery(product, null);
    }

    // autocompletetextview for the people name
    public String[] getName()
    {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("people", new String[] {"name"}, null, null, null, null, null);
        if(cursor.getCount() > 0)
        {
            String[] str = new String[cursor.getCount()+1];
            str[0] = "";
            int i=1;

            while (cursor.moveToNext()) 
            {
                str[i] = cursor.getString(cursor.getColumnIndex("name"));
                i++;
            }
            return str;
        }
        else
        {
            return new String[] {};
        }
    }

    // autocompletetextview for the product name
        public String[] getproduct()
        {
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.query("liverate", new String[] {"product_name"}, null, null, null, null, null);
            if(cursor.getCount() > 0)
            {
                String[] str = new String[cursor.getCount()+1];
                str[0] = "";
                int i=1;

                while (cursor.moveToNext()) 
                {
                    str[i] = cursor.getString(cursor.getColumnIndex("product_name"));
                    i++;
                }
                return str;
            }
            else
            {
                return new String[] {};
            }
        }

    // truncating the all the tables value
    public void truncate()
    {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE from liverate");
        db.execSQL("DELETE from people");
        db.execSQL("DELETE from transection");
        db.execSQL("DELETE from total_payment");
        db.execSQL("DELETE from everyday_transection");
    }

}

这是我试图获取数据并在SimpleAdapter中显示它的类

public class ProducTransection extends Activity 
    {
        AutoCompleteTextView actv;
        TextView datefrom, dateto;
        Button getbtn;
        ArrayAdapter<String> adapname;
        Cursor cl;
        HashMap<String, String> detail = new HashMap<String, String>();
        ArrayList<HashMap<String, String>> detailList;
        ListView lv1;
        DatabaseHandler db = new DatabaseHandler(ProducTransection.this);

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.producttransection);

        actv = (AutoCompleteTextView)findViewById(R.id.ptname);
        datefrom = (TextView)findViewById(R.id.ptfromdate);
        dateto = (TextView)findViewById(R.id.pttodate);
        getbtn = (Button)findViewById(R.id.ptgetbtn);
        lv1 = (ListView)findViewById(R.id.ptlist);

        Calendar dat = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String formdate = sdf.format(dat.getTime());

        datefrom.setText(formdate);
        dateto.setText(formdate);

        String[] name = db.getName();
        adapname = new ArrayAdapter<String>(ProducTransection.this, android.R.layout.simple_list_item_1, name);
        actv.setAdapter(adapname);

        getbtn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                try
                {
                detailList = new ArrayList<HashMap<String, String>>();
                cl = db.getproducttransection(actv.getText().toString(), datefrom.getText().toString(), dateto.getText().toString());

                cl.moveToFirst();
                //do
                for(int i=0;i<=cl.getColumnCount();i++)
                {
                    String date = cl.getString(cl.getColumnIndex("date"));
                    String product = cl.getString(cl.getColumnIndex("product_name"));
                    String price = cl.getString(cl.getColumnIndex("price"));
                    String Qty = cl.getString(cl.getColumnIndex("qty"));

                    detail.put("dat", date);
                    detail.put("pro", product);
                    detail.put("pri", price);
                    detail.put("qty", Qty);
                    detailList.add(detail);

                    SimpleAdapter simadap = new SimpleAdapter(ProducTransection.this,
                            detailList,
                            R.layout.producttransectionui,
                            new String[]{"dat","pro","pri","qty"},
                            new int[]{R.id.ptuidate,R.id.ptuiproduct,R.id.ptuiprice,R.id.ptuiqty});

                    lv1.setAdapter(simadap);
                }
                //while(cl.moveToNext());
                }
                catch (IndexOutOfBoundsException e) 
                {

                }
            }
        });
    }
}

现在我不知道哪里出错了请帮帮我

1 个答案:

答案 0 :(得分:0)

您通过调用cl.moveToFirst()

移至第一个结果

尝试这样:

 while (cl.moveToNext()) {
                String date = cl.getString(cl.getColumnIndex("date"));
                String product = cl.getString(cl.getColumnIndex("product_name"));
                String price = cl.getString(cl.getColumnIndex("price"));
                String Qty = cl.getString(cl.getColumnIndex("qty"));
               //populate your ArrayList
            }
 SimpleAdapter simadap = new SimpleAdapter(ProducTransection.this,
                        detailList,
                        R.layout.producttransectionui,
                        new String[]{"dat","pro","pri","qty"},
                        new int[]{R.id.ptuidate,R.id.ptuiproduct,R.id.ptuiprice,R.id.ptuiqty});

                lv1.setAdapter(simadap);

这是SimpleAdapter的简单tutorial。但在大多数情况下,您更愿意通过扩展BaseAdpapter来创建自己的适配器。