从游标获取特定数据

时间:2013-07-31 05:12:49

标签: java android

运行query后,我在cursor

中有如下数据
ID| TOPIC | TITLE | TYPE | NAME |
---------------------------------
1 | AB    | BCD   | ref  | Ferari|
----------------------------------
1 | AB    | BCD   | ref  | TOYOTA|
----------------------------------
2 | BC    | ABC   | notref| AUDI |
----------------------------------
2 | BC    | ABC    |notref| BMW  |

如何获取NAME

2 个答案:

答案 0 :(得分:0)

您可以获取NAME .........并且您可以将所有数据存储到arraylist ......并根据id值检索数据....... < / p>

试试这个

ArrayList datas=new ArrayList();
ArrayList list=new ArrayList();
 String StoreTitle = "", StoreName="";
       cursor.moveToFirst();
       do{

           int getID = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
           String Title = cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
           String StoreName= cursor.getString(cursor.getColumnIndexOrThrow("NAME"));

           if(StoreTitle.equalsIgnoreCase(Title)){
               list=new ArrayList();
               String getTopic = cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
               String getTitle = cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
               String getType = cursor.getString(cursor.getColumnIndexOrThrow("TYPE"));
               String getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
               String name=StoreName+" "+getName;
               list.add(getTopic);
               list.add(getTitle );
               list.add(getType );
               list.add(name);
               datas.remove(getID);
               datas.add(getID ,list);
           }
           else
           {
               list=new ArrayList();
               String getTopic = cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
               String getTitle = cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
               String getType = cursor.getString(cursor.getColumnIndexOrThrow("TYPE"));
               String getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
               String name=StoreName+" "+getName;
               list.add(getTopic);
               list.add(getTitle );
               list.add(getType );
               list.add(name);
               datas.add(getID ,list);
           }
           StoreTitle = Title;

     }while(cursor.moveToNext());

答案 1 :(得分:0)

  

只需修改其他部分,跟踪位置变量

else
{
String name=arraylist[position].getName;
name=name+" "+cursor.getString(cursor.getColumnIndexOrThrow("Name"));
arraylist.setname(name);
}