环地图为Android SQLite数据库 [英] Loop a map through android SQLite database

查看:128
本文介绍了环地图为Android SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有地图通过SQLlite数据库迭代,我想要的结果到EDITTEXT在另一个活动。我得到的格式不正确。这里是我的code。

 公开的ArrayList<&HashMap的LT;字符串,字符串>> getAllS()
{
    ArrayList的<&HashMap的LT;字符串,字符串>> SS;    SS =新的ArrayList<&HashMap的LT;字符串,字符串>>();    查询字符串=SELECT * FROM表,其中_ID ='+的getId()+';
    SQLiteDatabase数据库= this.getWritableDatabase();    光标光标= database.rawQuery(查询,NULL);
    如果(cursor.moveToFirst()){
        做{
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
            map.put(1,cursor.getString(0));
            map.put(2,cursor.getString(1));
            map.put(3,cursor.getString(2));
            map.put(4,cursor.getString(3));
            map.put(5,cursor.getString(4));
            Ss.add(地图);
        }而(cursor.moveToNext());
    }
    返回SS;
}

现在我得到的结果是

  [{1 = A,2 = B,3 = C,4 = D,5 = E} {1 = A,2 = B,3 = C,4 = D ,5 = E}]

我想要的结果是这样的:

 键:1价值:一
键:2值:B
键:3值:C
键:4值:D
关键字:5的价值:电子


解决方案

你好我假设你的查询是这样的。

  FROM表,其中_ID ='+的getId()+'SELECT价值; //只返回一列

和尝试这个code
HashMap中也是一个列表所以没有必要采取ArrayList的,除非您的具体要求

试试这个code :)

 公开的HashMap<字符串,字符串> getAllS()
{
    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();    查询字符串=SELECT * FROM表,其中_ID ='+的getId()+';
    SQLiteDatabase数据库= this.getWritableDatabase();    光标光标= database.rawQuery(查询,NULL);    INT I = 0;
    如果(光标=空&放大器;!cursor.moveToFirst()){
         int值= cursor.getColumnIndex(值); //节省了执行时间
         做{
              我++
              map.put(+ I,cursor.getString(VALUE));
            }而(cursor.moveToNext());
    }
    返回地图;
}

I have a map to iterate through a database in SQLlite, I want the result into Edittext in another activity. The format I am getting is not correct. Here is my code.

public  ArrayList<HashMap<String, String>> getAllS()
{
    ArrayList<HashMap<String, String>> Ss;

    Ss = new ArrayList<HashMap<String, String>>();

    String query ="SELECT * FROM table where _ID='"+getId()+"'";
    SQLiteDatabase database = this.getWritableDatabase();

    Cursor cursor =database.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("1", cursor.getString(0));
            map.put("2", cursor.getString(1));
            map.put("3", cursor.getString(2));
            map.put("4", cursor.getString(3));
            map.put("5", cursor.getString(4));
            Ss.add(map);
        } while (cursor.moveToNext());
    }
    return Ss;
}

The Result I am getting now is

[{1=a,2=b,3=c,4=d,5=e} {1=a,2=b,3=c,4=d,5=e} ]

I want the result to be like this:

Key : 1 Value : a
Key : 2 Value : b
Key : 3 Value : c
Key : 4 Value : d
Key : 5 Value : e

解决方案

Hi i am assuming that your query is like this

SELECT value FROM table where _ID='"+getId()+"'"; // only return one column 

and try this code Hashmap is also one list so no need to take ArrayList unless your specific requirement

try this code :)

public  HashMap<String, String> getAllS()
{
    HashMap<String, String>   map = new HashMap<String, String>();

    String query ="SELECT * FROM table where _ID='"+getId()+"'";
    SQLiteDatabase database = this.getWritableDatabase();

    Cursor cursor =database.rawQuery(query, null);

    int i=0;
    if (cursor != null & cursor.moveToFirst()) {
         int VALUE= cursor.getColumnIndex("value");// saves execution time
         do {
              i++
              map.put(""+i, cursor.getString(VALUE));
            } while (cursor.moveToNext());
    }
    return map;
}

这篇关于环地图为Android SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆