Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 5.56 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to get data from my database and put it on a ListView that is clickable?
  2. Button ViewBack;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5.     // TODO Auto-generated method stub
  6.  
  7.     super.onCreate(savedInstanceState);
  8.     setContentView(R.layout.sqlview);
  9.  
  10.     ViewBack=(Button) findViewById(R.id.bVBack);
  11.     TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
  12.     ViewBack.setOnClickListener(this);
  13.  
  14.     HotOrNot info = new HotOrNot(this);
  15.     info.open();
  16.     String data = info.geData();
  17.     info.close();
  18.     tv.setText(data);
  19.  
  20. }
  21. @Override
  22. public void onClick(View v) {
  23.     // TODO Auto-generated method stub
  24.     finish();
  25.  
  26. }
  27.        
  28. <ScrollView
  29.       android:layout_width="fill_parent"
  30.       android:layout_height="match_parent" >      
  31. <TextView
  32.     android:id="@+id/tvSQLinfo"
  33.     android:layout_width="fill_parent"
  34.     android:layout_height="fill_parent"
  35.     android:layout_marginTop="10dp"
  36.     android:text="get info from db" />
  37. </ScrollView>
  38.        
  39. ListView lv = (ListView) findViewById(R.id.listMe);
  40. ListAdapter listAD = new ListAdapter(this,R.id.listMe, tv);
  41.  lv.setAdapter(listAD);
  42.        
  43. ListAdapter listAD = new SimpleCursorAdapter (this,R.id.listMe,tv, null, null);
  44.        
  45. public static final String KEY_ROWID= "_id";
  46. public static final String KEY_NAME= "persons_name";
  47. public static final String KEY_HOTNESS= "presons_hotness";
  48.  
  49. private static final String DATABASE_NAME= "HotOrNotdb";
  50. public static final String DATABASE_TABLE= "peopleTable";
  51. private static final int DATABASE_VERSION= 1;
  52.  
  53. private DbHelper ourHelper;
  54. private final Context ourContext;
  55. private SQLiteDatabase ourDatabase;
  56.  
  57. private static class DbHelper extends SQLiteOpenHelper{
  58.  
  59.     public DbHelper(Context context) {
  60.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  61.         // TODO Auto-generated constructor stub
  62.     }
  63.  
  64.  
  65.     @Override
  66.     public void onCreate(SQLiteDatabase db) {
  67.         // TODO Auto-generated method stub
  68.  
  69.         db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
  70.                 KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
  71.                 KEY_NAME + " TEXT NOT NULL, " +
  72.                 KEY_HOTNESS + " TEXT NOT NULL);"
  73.         );  
  74.  
  75.     }
  76.  
  77.     @Override
  78.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  79.         // TODO Auto-generated method stub
  80.             db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE);
  81.             onCreate(db);
  82.     }
  83.  
  84. }
  85. public HotOrNot(Context c){
  86.     ourContext=c;
  87. }
  88.  
  89. public HotOrNot open() throws SQLiteException{
  90. ourHelper = new DbHelper(ourContext);
  91. ourDatabase =ourHelper.getWritableDatabase();
  92.  
  93. return this;  }
  94.  
  95. public void close(){
  96.     ourHelper.close();
  97. }
  98.  
  99. public long createEntry(String name, String hotness) {
  100.     // TODO Auto-generated method stub
  101.     ContentValues cv = new ContentValues();
  102.     cv.put(KEY_NAME, name);
  103.     cv.put(KEY_HOTNESS, hotness);
  104.     return ourDatabase.insert(DATABASE_TABLE, null, cv);
  105.  
  106. }
  107.  
  108. public String geData() {
  109.     // TODO Auto-generated method stub
  110.     String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
  111.     Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
  112.     String result ="";
  113.     int iRow=c.getColumnIndex(KEY_ROWID);
  114.     int iName=c.getColumnIndex(KEY_NAME);
  115.     int iHotness=c.getColumnIndex(KEY_HOTNESS);
  116.  
  117.     for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
  118.         result=result + c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness)+"n";
  119.     }
  120.     return result;
  121.  
  122. }
  123. public String getName(long l) throws SQLiteException{
  124.     // TODO Auto-generated method stub
  125.     String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
  126.     Cursor c= ourDatabase.query(DATABASE_TABLE, columns,  KEY_ROWID+"="+l, null, null, null, null);
  127.     if (c!=null){
  128.         c.moveToFirst();
  129.  
  130.         String name=c.getString(1);
  131.         return name;
  132.     }
  133.     return null;
  134. }
  135.  
  136. public String getHotness(long l)throws SQLiteException {
  137.     // TODO Auto-generated method stub
  138.     String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
  139.     Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null);
  140.     if (c!=null){
  141.         c.moveToFirst();
  142.  
  143.         String hotness=c.getString(2);
  144.         return hotness;
  145.     }
  146.     return null;
  147. }
  148.        
  149. public ArrayList<String> geData() {
  150.     // TODO Auto-generated method stub
  151.     String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
  152.     Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
  153.     ArrayList<String> result = new ArrayList<String>();
  154.     int iRow=c.getColumnIndex(KEY_ROWID);
  155.     int iName=c.getColumnIndex(KEY_NAME);
  156.     int iHotness=c.getColumnIndex(KEY_HOTNESS);
  157.  
  158.     for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
  159.         result.add(c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness));
  160.     }
  161.     return result;
  162. }
  163.        
  164. <?xml version="1.0" encoding="utf-8"?>
  165. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  166.     android:layout_width="match_parent"
  167.     android:layout_height="match_parent"
  168.     android:orientation="vertical" >
  169.  
  170.     <Button
  171.         android:id="@+id/bVBack"
  172.         android:layout_width="wrap_content"
  173.         android:layout_height="wrap_content"
  174.         android:text="Something" />
  175.  
  176.     <ListView
  177.         android:id="@+id/listMe"
  178.         android:layout_width="fill_parent"
  179.         android:layout_height="wrap_content" />
  180.  
  181. </LinearLayout>
  182.        
  183. //...
  184. ListView lv = (ListView) findViewById(R.id.listMe);
  185. HotOrNot info = new HotOrNot(this);
  186. info.open();
  187. ArrayList<String> data = info.geData();
  188. info.close();
  189. lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item1, data));