title: Displaying values from database in TextView [Android]
body: <p>I'm trying to display values from a database activity that is created within the application itself. The values were originally parsed from XML files and then stored into the DB. I have done a good amount of searching and tried several methods of reaching a solution but I seem to be stuck. If someone could help me it out it would be great!</p>
  
  <p>Q, (I)The values must be displayed in a TextView once an item from a ListView is selected. To do this I have set up an onItemClickListener in the ListView Activity(ListRSSItemsActivity) and an intent to start the new activity, as well as created a corresponding Layout file. I'm more or less familiar with how to get a TextView working. But, I'm unsure of what to be putting in the New Activity(ArticleActivity) file now. 
  (II) When I click on the item and the new window opens, it displays a blank screen.</p>
  
  <p>I have attached the new activity class and the layout file.</p>
  
  <p><strong>ArticleActivity:</strong></p>
  
  <pre><code>    public class ArticleActivity extends Activity{
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.article_view);
  
          String title, description;
  
          title=this.getIntent().getStringExtra("TAG_TITLE");
          description=this.getIntent().getStringExtra("TAG_DESCRIPTION");     
  }
  }
  </code></pre>
  
  <p><strong>article_view.xml:</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;!-- Article Title --&gt;
      &lt;TextView android:id="@+id/title"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingTop="10dp"
          android:paddingBottom="8dp"
          android:textSize="18sp"
          android:textStyle="bold"
          android:textColor="#dc6800"/&gt;
  
      &lt;!-- Article Content --&gt;
      &lt;TextView android:id="@+id/description"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textColor="#acacac"
          android:layout_below="@id/title"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>ListRSSItemsActivity:</strong></p>
  
  <pre><code>public class ListRSSItemsActivity extends ListActivity {
  
      // Progress Dialog
      private ProgressDialog pDialog;
  
      // Array list for list view
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; rssItemList = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();
  
      RSSParser rssParser = new RSSParser();
  
      List&lt;RSSItem&gt; rssItems = new ArrayList&lt;RSSItem&gt;();
  
      RSSFeed rssFeed;
  
      public static String TAG_TITLE = "title";
      private static String TAG_LINK = "link";
      private static String TAG_DESCRIPTION = "description";
      private static String TAG_PUBDATE = "pub_date";
      private static String TAG_GUID = "guid"; // not used
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  //        setContentView(R.layout.rss_item_list);
  
          // get intent data
          Intent i = getIntent();
  
          // SQLite Row id
          Integer site_id = Integer.parseInt(i.getStringExtra("id"));
  
          // Getting Single website from SQLite
          RSSDatabaseHandler rssDB = new RSSDatabaseHandler(getApplicationContext());
  
  
          WebSite site = rssDB.getSite(site_id);
          String rss_link = site.getRSSLink();
  
          /**
           * Calling a backgroung thread will loads recent articles of a website
           * @param rss url of website
           * */
          new loadRSSFeedItems().execute(rss_link);
  
          // selecting single ListView item
          ListView lv = getListView();
  
          // Launching new screen on Selecting Single ListItem
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
  
                  // getting listview
                  View v = (View)view.getParent(); 
                  TextView tv = (TextView)v.findViewById(R.id.title);
                  String title = tv.getText().toString();
                  String description = ((TextView) view.findViewById(R.id.link)).getText().toString();
  
                  // Starting new intent
                  Intent ia = new Intent(getApplicationContext(), ArticleActivity.class);
                  ia.putExtra("TAG_TITLE", title);
                  ia.putExtra("TAG_DESCRIPTION", description);
                  startActivity(ia);              
              }
          });
      }
  
      /**
       * Background Async Task to get RSS Feed Items data from URL
       * */
      class loadRSSFeedItems extends AsyncTask&lt;String, String, String&gt; {
  
          /**
           * Before starting background thread Show Progress Dialog
           * */
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(
                      ListRSSItemsActivity.this);
              pDialog.setMessage("Loading recent articles...");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(false);
              pDialog.show();
          }
  
          /**
           * getting all recent articles and showing them in listview
           * */
          @Override
          protected String doInBackground(String... args) {
              // rss link url
              String rss_url = args[0];
  
              // list of rss items
              // using GET method to obtain rssfeeditems. NOTICE: only from RSS_URL here!
              rssItems = rssParser.getRSSFeedItems(rss_url);
  
              // looping through each item
              for(RSSItem item : rssItems){
                  // creating new HashMap
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                  // adding each child node to HashMap key =&gt; value
                  map.put(TAG_TITLE, item.getTitle());
                  map.put(TAG_LINK, item.getLink());
                  map.put(TAG_PUBDATE, item.getPubDate());
  
                  String description = item.getDescription();
                  // taking only 200 chars from description
                  if(description.length() &gt; 100){
                      description = description.substring(0, 97) + "..";
                  }
                  map.put(TAG_DESCRIPTION, description);
  
  
                  // adding HashList to ArrayList
                  rssItemList.add(map);
              }
  
              // updating UI from Background Thread
              runOnUiThread(new Runnable() {
                  public void run() {
                      /**
                       * Updating parsed items into listview
                       * */
                      ListAdapter adapter = new SimpleAdapter(
                              ListRSSItemsActivity.this,
                              rssItemList, R.layout.rss_item_list_row,
                              new String[] { TAG_LINK, TAG_TITLE, TAG_PUBDATE, TAG_DESCRIPTION },
                              new int[] { R.id.page_url, R.id.title, R.id.pub_date, R.id.link });
  
                      // updating listview
                      setListAdapter(adapter);
                  }
              });
              return null;
          }
  
          /**
           * After completing background task Dismiss the progress dialog
           * **/
          protected void onPostExecute(String args) {
              // dismiss the dialog after getting all products
              pDialog.dismiss();
          }
      }
  }
  </code></pre>
  
  <p><strong>Database Activity:</strong></p>
  
  <pre><code>    public class RSSDatabaseHandler extends SQLiteOpenHelper{
  
      // Database Version
      private static final int DATABASE_VERSION = 1;
  
      // Database Name
      private static final String DATABASE_NAME = "rssReader";
  
      // Contacts table name
      private static final String TABLE_RSS = "websites";
  
      // Contacts Table Columns names
      private static final String KEY_ID = "id";
      private static final String KEY_TITLE = "title";
      private static final String KEY_LINK = "link";
      private static final String KEY_RSS_LINK = "rss_link";
      private static final String KEY_DESCRIPTION = "description";
      private static final String KEY_PUBDATE = "pub_date";
  
      public RSSDatabaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      // Creating Tables
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_RSS_TABLE = "CREATE TABLE " + TABLE_RSS + "(" + KEY_ID
                  + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT," + KEY_LINK
                  + " TEXT," + KEY_RSS_LINK + " TEXT," + KEY_DESCRIPTION
                  + " TEXT," + KEY_PUBDATE + " TEXT" + ")";
          db.execSQL(CREATE_RSS_TABLE);
      }
  
      // Upgrading database
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // Drop older table if existed
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_RSS);
  
          // Create tables again
          onCreate(db);
      }
  
      /**
       * Adding a new website in websites table Function will check if a site
       * already existed in database. If existed will update the old one else
       * creates a new row
       * */
      public void addSite(WebSite site) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_TITLE, site.getTitle()); // site title
          values.put(KEY_LINK, site.getLink()); // site url
          values.put(KEY_RSS_LINK, site.getRSSLink()); // rss link url
          values.put(KEY_DESCRIPTION, site.getDescription()); // site description
          values.put(KEY_PUBDATE, site.getPubDate());
  
          // Check if row already existed in database
          if (!isSiteExists(db, site.getRSSLink())) {
              // site not existed, create a new row
              db.insert(TABLE_RSS, null, values);
              db.close();
          } else {
              // site already existed update the row
              updateSite(site);
              db.close();
          }
      }
  
      /**
       * Reading all rows from database
       * */
      public List&lt;WebSite&gt; getAllSites() {
          List&lt;WebSite&gt; siteList = new ArrayList&lt;WebSite&gt;();
          // Select All Query
          String selectQuery = "SELECT  * FROM " + TABLE_RSS
                  + " ORDER BY id DESC";
  
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery(selectQuery, null);
  
          // looping through all rows and adding to list
          if (cursor.moveToFirst()) {
              do {
                  WebSite site = new WebSite();
                  site.setId(Integer.parseInt(cursor.getString(0)));
                  site.setTitle(cursor.getString(1));
                  site.setLink(cursor.getString(2));
                  site.setRSSLink(cursor.getString(3));
                  site.setDescription(cursor.getString(4));
                  site.setPubDate(cursor.getString(5));
                  // Adding contact to list
                  siteList.add(site);
              } while (cursor.moveToNext());
          }
          cursor.close();
          db.close();
  
          // return contact list
          return siteList;
      }
  
      /**
       * Updating a single row row will be identified by rss link
       * */
      public int updateSite(WebSite site) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_TITLE, site.getTitle());
          values.put(KEY_LINK, site.getLink());
          values.put(KEY_RSS_LINK, site.getRSSLink());
          values.put(KEY_DESCRIPTION, site.getDescription());
          values.put(KEY_PUBDATE, site.getPubDate());
  
          // updating row return
          int update = db.update(TABLE_RSS, values, KEY_RSS_LINK + " = ?",
                  new String[] { String.valueOf(site.getRSSLink()) });
          db.close();
          return update;
  
      }
  
      /**
       * Reading a row (website) row is identified by row id
       * */
      // Declaring 
      public WebSite getSite(int id) {
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.query(TABLE_RSS, new String[] { KEY_ID, KEY_TITLE,
                  KEY_LINK, KEY_RSS_LINK, KEY_DESCRIPTION, KEY_PUBDATE }, KEY_ID + "=?",
                  new String[] { String.valueOf(id) }, null, null, null, null);
          if (cursor != null)
              cursor.moveToFirst();
  
          WebSite site = new WebSite(cursor.getString(1), cursor.getString(2),
                  cursor.getString(3), cursor.getString(4), cursor.getString(5));
  
          site.setId(Integer.parseInt(cursor.getString(0)));
          site.setTitle(cursor.getString(1));
          site.setLink(cursor.getString(2));
          site.setRSSLink(cursor.getString(3));
          site.setDescription(cursor.getString(4));
          site.setPubDate(cursor.getString(5));
          cursor.close();
          db.close();
          return site;
      }
  
      /**
       * Deleting single row
       * */
      public void deleteSite(WebSite site) {
          SQLiteDatabase db = this.getWritableDatabase();
          db.delete(TABLE_RSS, KEY_ID + " = ?",
                  new String[] { String.valueOf(site.getId())});
          db.close();
      }
  
      /**
       * Checking whether a site is already existed check is done by matching rss
       * link
       * */
      public boolean isSiteExists(SQLiteDatabase db, String rss_link) {
  
          Cursor cursor = db.rawQuery("SELECT 1 FROM " + TABLE_RSS
                  + " WHERE rss_link = '" + rss_link + "'", new String[] {});
          boolean exists = (cursor.getCount() &gt; 0);
          return exists;
      }
  }
  </code></pre>