从android中的SQLite数据库中检索数据

时间:2013-11-25 12:20:10

标签: android sqlite

我在SQLite数据库中使用thana_name,mobile_no,telephone_no,district_name列创建了一个表名“thanas”。现在还插入数据我想要检索与zone_name匹配的3列数据。但在LogCat中只有第一列数据显示。我还试图用这些数据制作一个ArrayAdapter,以便在ListView中显示它。在listview中只显示thana_name。我在onCreat()方法中调用了populateArrayListOfDhakaDistrict()这个方法。一切都是正确的,但它只显示第一列。

public static final String SELECT_THANA_NAME_MOBILE_TELIPHONE = "select thana_name, mobile_no, telephone_no from thanas where thanas.district_name like '%Dhaka%' ;";

    public Cursor getThanaInfo() {
             return db.rawQuery(SELECT_THANA_NAME_MOBILE_TELIPHONE, null);
         }


    public void populateArrayListOfDhakaDistrict () {
            List <String> thanaInfoList = getAllThanaInfo();


            System.out.println("unsorted: "+thanaInfoList);
            ListView listView = getListView();

            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            //listView.setTextFilterEnabled(true);      
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,thanaInfoList);     
            listView.setAdapter(arrayAdapter);
        }

        public List <String> getAllThanaInfo() {
            dbOperation.open();
            //List <String> devisionInfoList = new ArrayList<String> ();
            Cursor c= dbOperation.getThanaInfo();
            c.moveToFirst();
            while (! c.isAfterLast()) {
                String thanaInfo= new String(c.getString(0));
                //String teleInfo = new String (c.getColumnIndex(thanaInfo));
                thanaInfoList.add(thanaInfo);

                System.out.println("Thana is:"+thanaInfo);
                c.moveToNext();
            }
            c.close();
            return thanaInfoList;

        }

3 个答案:

答案 0 :(得分:1)

c.moveToFirst();
while (!c.isAfterLast()) {
    String thanaInfo = c.getString(c.getColumnIndex("thana_name"));
    thanaInfoList.add(thanaInfo);

    System.out.println("Thana is:" + thanaInfo);
    c.moveToNext();
}
c.close();
return thanaInfoList;

答案 1 :(得分:0)

试试此代码

public static final String SELECT_THANA_NAME_MOBILE_TELIPHONE = "select thana_name, mobile_no, telephone_no from thanas where thanas.district_name like '%Dhaka%' ;";
private             List <String> thanaInfoList ;


    public Cursor getThanaInfo() {
             return db.rawQuery(SELECT_THANA_NAME_MOBILE_TELIPHONE, null);
         }


    public void populateArrayListOfDhakaDistrict () {


            System.out.println("unsorted: "+thanaInfoList);
            ListView listView = getListView();

            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            //listView.setTextFilterEnabled(true);      
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,thanaInfoList);     
            listView.setAdapter(arrayAdapter);
        }

        public void getAllThanaInfo() {
            dbOperation.open();
            List <String> thanaInfoList = new List<String>;
            Cursor c= dbOperation.getThanaInfo();
             if ( C != null) {
             try {
                 while (c.moveToNext()) {
                      thanaInfoList.add(c.getString(0));

                }
               } finally {
                   c.close();
    }

        }

答案 2 :(得分:-1)

由于您已经知道返回了哪些列,因此您不需要getColumnIndex,只需引用正确的列索引:

 String thanaInfo = c.getString(0);