-1

我正在处理一个有很多表并且一个表包含多行的项目。如何从数据库中获取数据并使用SimpleAdapter?

我曾尝试使用游标,但我只获取存储在数据库中的最后一行值。

尝试使用时,ArrayList<Hashmap<String, String>>我得到一个NullPointerException,即使使用 try catch 块也无法解决。

这是获取它的代码

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) {

                }
            }
        });
    }
}

现在,如果我错了,请帮助我获取数据并将其显示在 SimpleAdapter 中。

4

2 回答 2

0

你想在哪里从 sqllite 或 mysql 接收数据
如果它的 mysql 然后使用 php 检索数据并以 json 格式回显(谷歌搜索)然后使用JSON解析你的 android 应用程序中的值
,然后用 listview 显示

于 2014-08-25T18:08:45.833 回答
0

我建议您使用 ORM 库,因为您正在处理许多表。这将减轻你的查询和东西的工作。

要了解什么是 ORM,请访问

http://en.wikipedia.org/wiki/Object-relational_mapping

并下载适用于 Android 的 ORM 库

http://ormlite.com/

ORM 库教程,

http://ormlite.com/android/examples/

于 2014-08-25T18:30:29.953 回答