【问题标题】:Getting row data from database and retrieving it in Android从数据库中获取行数据并在 Android 中检索它
【发布时间】:2014-10-18 22:11:43
【问题描述】:

我正在处理一个包含许多表且一个表包含多行的项目。如何从数据库中获取数据并使用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 中显示。

【问题讨论】:

  • 发布你的代码和日志
  • 这篇文章很清楚了解如何使用数据库:vogella.com/tutorials/AndroidSQLite/article.html
  • @mmlooloo 我不允许上传没有足够积分的图片 bcs
  • @Distwo 我已经看过教程,但没有帮助
  • 我不想让你上传图片,我想发布你的代码。就像您键入它一样,您可以复制并粘贴您的代码并使用{} 符号使其变得更好,您也可以保存您的 logcat 并粘贴它;-)

标签: android sqlite nullpointerexception simpleadapter null-pointer


【解决方案1】:

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

要了解什么是 ORM,请转到

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

并下载适用于 Android 的 ORM 库

http://ormlite.com/

ORM 库教程,

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • sqllite我写错mysql了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2016-09-16
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多