What I learned today, February 23, 2015 (mostly Android)

This is a collection of notes about what I learned today, February 23, 2015. Most of it is about Android.

Back to top

I need to refresh my cursor data set before calling notifyDataSetChanged

When (a) adding, editing, or deleting items in a ListView and (b) using a CursorAdapter, I need to update my cursor object before calling notifyDataSetChanged.

I created this method, which I call from my fragment’s onResume method:

private void refreshQuoteCursorDataSet() {
    quoteCursor.requery();
    ((QuoteCursorAdapter)getListAdapter()).notifyDataSetChanged();
}
Back to top

I can refresh the data set in either onResume or onActivityResult

After an add, edit, or delete operation on a ListView I currently call onResume to refresh my data set and view, but I can optionally call onActivityResult instead. Here’s the source code for my current onResume method:

@Override
public void onResume() {
    super.onResume();
    refreshQuoteCursorDataSet();
}
Back to top

GridView scrolls automatically

There’s no need to wrap a GridView with a scrolling container, it scrolls automatically.

Back to top

Craziness: I had to add an id before using it in a layout

In the craziest thing of the day, I had to add an id in a layout before I could use it. This is shown in the following XML code, where I add the id for putQuoteHere before I actually define that element (a TextView):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.alvinalexander.myapp.LandingPageActivity.ShowFullQuoteFragment"
                >

    <!-- i place the background image on this imageview. it does the fitting/cropping work. -->
    <!-- craziness: i need to create the id before using it in the TextView -->
    <ImageView android:id="@+id/landingPageBGImage"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:scaleType="centerCrop"
               android:layout_alignBottom="@+id/putQuoteHere" />

    <TextView
        android:id="@id/putQuoteHere"
        android:text="@string/theFullText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:padding="48dp"
        android:background="#ecffffff"
        />

</RelativeLayout>

Until then I was getting a “No resource found that matches the given name” error message regarding that id. I found the solution at this SO url.

Back to top

Github has private repositories for as low as $7/month

In related news, I started paying Github $7/month so I can have private repositories. For that price for a personal account they let you have five private repos.

Back to top

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.