2015-06-30 69 views
0

我只是在学习android编程,现在我正在制作一个使用数据库的应用程序。现在我正在使用SQLite来存储数据。在Android中从SQLite数据库填充ListView

我已经成功地创建了可以在数据库中存储/插入数据的应用程序。它的工作和我使用根资源管理器检查我的数据库。

现在第二个活动让我们说DisplayRecords,我想显示我的记录到列表视图,这将动态填充。我如何通过编程来实现这一点。

这是我第二次活动的代码。

public class SearchResult extends ActionBarActivity { 

ListView ls; 
SQLiteDatabase db; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search_result); 

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    ls = (ListView)findViewById(R.id.displayListView); 

Cursor c = db.rawQuery("SELECT * FROM records",null); 

    if(c.getCount()==0){ 
     Toast.makeText(getApplicationContext(), "No Records Found", Toast.LENGTH_LONG).show(); 
     setContentView(R.layout.activity_main); 
    } 

    StringBuffer buffer = new StringBuffer(); 

    while(c.moveToNext()){ 

     buffer.append("Topic"+c.getString(0)); 
     buffer.append("Description"+c.getString(1)); 
    } 

    //now how can i put the fetched value in ListView 

} 

谁能帮我

感谢

+0

这是** **骨架。 **肉在哪里? –

+0

@DerGolem我想肉只因为我有骨架 – Mustakim0077

+0

但它不工作就像这样,在这里:'StackOverflow不是谷歌替代'。你应该搜索一个教程并遵循它。如果你弄得一团糟,那就回过头来看看你的烂摊子,然后我们可以做些什么来解决它。 –

回答

0

类握着你的数据

public class Naats_VO { 

    private String id; 
    private String person_id; 
    private String title; 
    private String keywords; 
    private String language; 
    private String type; 
    private String length; 
    private String url; 
    private String fav_count; 
    private String hit_count; 
    private String release_date; 

    public Naats_VO() { 
    } 

    public Naats_VO(String id, String person_id, String title, String keywords, 
      String language, String type, String length, String url, 
      String fav_count, String hit_count, String release_date) { 
     super(); 
     this.id = id; 
     this.person_id = person_id; 
     this.title = title; 
     this.keywords = keywords; 
     this.language = language; 
     this.type = type; 
     this.length = length; 
     this.url = url; 
     this.fav_count = fav_count; 
     this.hit_count = hit_count; 
     this.release_date = release_date; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getPerson_id() { 
     return person_id; 
    } 

    public void setPerson_id(String person_id) { 
     this.person_id = person_id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getKeywords() { 
     return keywords; 
    } 

    public void setKeywords(String keywords) { 
     this.keywords = keywords; 
    } 

    public String getLanguage() { 
     return language; 
    } 

    public void setLanguage(String language) { 
     this.language = language; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getLength() { 
     return length; 
    } 

    public void setLength(String length) { 
     this.length = length; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getFav_count() { 
     return fav_count; 
    } 

    public void setFav_count(String fav_count) { 
     this.fav_count = fav_count; 
    } 

    public String getHit_count() { 
     return hit_count; 
    } 

    public void setHit_count(String hit_count) { 
     this.hit_count = hit_count; 
    } 

    public String getRelease_date() { 
     return release_date; 
    } 

    public void setRelease_date(String release_date) { 
     this.release_date = release_date; 
    } 

} 

Actvity其中包含的ListView

  private List<Naats_VO> naats_list; 
      private Naat_Model db; 
      Naats_Adapter adapter ; 

      // Initilizae in OnCreate 
      naats_list; = new ArrayList<Naats_VO>(); 
      // Naat_Model is class which communicate with my db. 
      db = new Naat_Model(this); 

      naats_list = db.getFavorites(); 

      adapter = new Naats_Adapter(this, naats_list); 
      listView.setAdapter(adapter); 

Naat_Model

public class Naat_Model extends _BaseModel { 

    // Contacts table name persons 
    public static final String TABLE_NAAT = "naats"; 

    // persons Table Columns names 
    public static final String KEY_ID = "id"; 
    public static final String KEY_PERSON_ID1 = "person_id"; 
    public static final String KEY_TITLE = "title"; 
    public static final String KEY_KEYWORDS = "keywords"; 
    public static final String KEY_LANG1 = "language"; 
    public static final String KEY_TYPE = "type"; 
    public static final String KEY_LENGTH = "length"; 
    public static final String KEY_URL = "url"; 
    public static final String KEY_FAV_COUNT = "fav_count"; 
    public static final String KEY_HIT_COUNT = "hit_count"; 
    public static final String KEY_RELEASE_DATE = "release_date"; 
    public static final String KEY_FAVR = "favorite"; 
    public static final String KEY_RECENT = "recent"; 
    public static final String KEY_DOWNLOAD_STATUS = "status"; 
    public static final String KEY_DOWNLOAD_id = "downloader_id"; 
    public static final String KEY_NOTIFICATION_ID = "notifiaction_id"; 
    public static final String KEY_CREATED_AT = "created_at"; 

    private SQLiteDatabase db; 
    private Context _context; 

    public Naat_Model(Context context) { 
     super(TABLE_NAAT, context); 
     // TODO Auto-generated constructor stub 
     _context = context; 
    } 

    public List<Naats_VO> getFavorites() { 

     List<Naats_VO> Naats_list = new ArrayList<Naats_VO>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_NAAT + " WHERE " 
       + KEY_FAVR + " = 1"; 

     Database dbHelper = new Database(_context); 
     this.db = dbHelper.getReadableDatabase(); 
     Cursor c = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (c.moveToFirst()) { 
      do { 
       Naats_VO naats = new Naats_VO(); 

       naats.setId(c.getString(c.getColumnIndex(KEY_ID))); 
       naats.setPerson_id(c.getString(c.getColumnIndex(KEY_PERSON_ID1))); 
       naats.setTitle(c.getString(c.getColumnIndex(KEY_TITLE))); 
       naats.setKeywords(c.getString(c.getColumnIndex(KEY_KEYWORDS))); 
       naats.setLanguage(c.getString(c.getColumnIndex(KEY_LANG1))); 
       naats.setType(c.getString(c.getColumnIndex(KEY_TYPE))); 
       naats.setLength(c.getString(c.getColumnIndex(KEY_LENGTH))); 
       naats.setUrl(c.getString(c.getColumnIndex(KEY_URL))); 
       naats.setFav_count(c.getString(c.getColumnIndex(KEY_FAV_COUNT))); 
       naats.setHit_count(c.getString(c.getColumnIndex(KEY_HIT_COUNT))); 
       naats.setRelease_date(c.getString(c 
         .getColumnIndex(KEY_RELEASE_DATE))); 

       // Adding contact to list 
       Naats_list.add(naats); 
      } while (c.moveToNext()); 
     } 
     db.close(); 
     c.close(); 
     // return contact list 
     return Naats_list; 
    } 

这个类将与应用程序连接你的数据库 http://i.stack.imgur.com/0fFtD.png

public class Database extends SQLiteAssetHelper { 
    private static final String DATABASE_NAME = "naat_collection"; 

    private static final int DATABASE_VERSION = 1; 

    public Database(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // super(context, DATABASE_NAME, 
     // context.getExternalFilesDir(null).getAbsolutePath(), null, 
     // DATABASE_VERSION); 

     } 

    } 
0

因此,在Android的数据库工作是大面积的,我会尽量给你一些方向。

首先,您应该使用ContentResolver而不是直接调用数据库来获取Cursor,这有助于避免UI挂在严格的查询上,因为ContentResolver在单独的线程中执行查询。

为了解决您的任务(使用数据填充ListView并在更改时更新它)Android提供了Loaders机制。

您必须在活动中使用LoaderManagergetLoaderManager方法。然后实现LoaderManager.LoaderCallbacks(我将把它作为loaderCollback)

loaderCallback方法onCreateLoader你应该创建特定CursorLoader(它会做所有的东西正确查询在单独的线程DB)

后初始化加载器loaderCallback方法onLoadFinished您将收到您的光标,您需要做的就是调用swapCursor ListView的CursorAdapter方法。最后是魔法:你必须写这样的东西

cursor.setNotificationUri(getActivity.getContentResolver(), queryUri) 

这都是从高度看。

有关详细信息,您可以检查此tutorial

+0

感谢您的回答。说真的,我不明白你说的。从你说的我得出的结论是,对我来说很难,我仍然需要学习很多关于数据处理的知识。 任何方式,谢谢你的时间和答案。 +1 – Mustakim0077

+0

只需调查答案的链接:)你也应该为答案投票,如果它没有评论,但在左侧切换有帮助;-) –

+0

我会检查它。我不能投票,因为我的声望不高于15,我无法投票。 – Mustakim0077

相关问题