Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Simple example activity that displays a list of all internal music files (ringtones etc).</p> <p><em>MyActivity.java</em></p> <pre><code>public class MyActivity extends Activity { private MyCursorAdapter mAdapter; // that's what we want to know from the database private static final String[] PROJECTION = new String[] { MediaStore.Audio.AudioColumns._ID, // 0 - _id must be present MediaStore.Audio.AudioColumns.TITLE, // 1 MediaStore.Audio.AudioColumns.DATA // 2 }; // those from above - no need for cursor.getColumnIndex() private static final int TITLE_IDX = 1; private static final int TEXT_IDX = 2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv = (ListView) findViewById(R.id.list_view); mAdapter = new MyCursorAdapter(this, TITLE_IDX, TEXT_IDX); lv.setAdapter(mAdapter); loadContent(); } // would be better to do in a Loader, AsyncTask, ... private void loadContent() { ContentResolver cr = getContentResolver(); Cursor c = cr.query( MediaStore.Audio.Media.INTERNAL_CONTENT_URI, PROJECTION, null, null, null ); mAdapter.changeCursor(c); } } </code></pre> <p><em>MyCursorAdapter.java</em><br> there is no real dependency on the Cursor in this class, it's much like the <code>SimpleCursorAdapter</code></p> <pre><code>public class MyCursorAdapter extends CursorAdapter { private final LayoutInflater mInflater; private final int mTitleIdx, mTextIdx; /** * Creates a new MyCursorAdapter. Set cursor via changeCursor / swapCursor * @param context &lt;code&gt;this&lt;/code&gt; will usually do * @param titleColumnIdx cursor columnindex to be displayed as title * @param textColumnIdx cursor columnindex to be displayed as text below */ public MyCursorAdapter(Context context, int titleColumnIdx, int textColumnIdx) { super(context, null, false); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mTitleIdx = titleColumnIdx; mTextIdx = textColumnIdx; } @Override public void bindView(View view, Context context, Cursor cursor) { TextView title = (TextView) view.findViewById(R.id.title); TextView text = (TextView) view.findViewById(R.id.text); title.setText(cursor.getString(mTitleIdx)); text.setText(cursor.getString(mTextIdx)); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View item = mInflater.inflate(R.layout.list_item, null); // could do static init here / attach holder / set onClickListeners, ... return item; } } </code></pre> <p><em>main.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list_view" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;!-- Preview: listitem=@layout/list_item --&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p><em>list_item.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="@android:style/TextAppearance.Large" android:textColor="@android:color/primary_text_dark" android:layout_marginTop="5dp" /&gt; &lt;TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="@android:style/TextAppearance.Small" android:textColor="@android:color/secondary_text_dark" android:singleLine="true" android:ellipsize="end" android:layout_marginBottom="5dp" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><em>What you get</em></p> <p><img src="https://i.stack.imgur.com/o6myq.png" alt="result"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload